Video editing with Python
Test properly 'blackwhite' FX using 'BitmapClip' (#1430)
Álvaro Mondéjar727276f84766
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.pyindex 10f44270..b6fdd4dc 100644--- a/moviepy/video/VideoClip.py+++ b/moviepy/video/VideoClip.py@@ -1336,6 +1336,19 @@ class TextClip(ImageClip):class BitmapClip(VideoClip):+ DEFAULT_COLOR_DICT = {+ "R": (255, 0, 0),+ "G": (0, 255, 0),+ "B": (0, 0, 255),+ "O": (0, 0, 0),+ "W": (255, 255, 255),+ "A": (89, 225, 62),+ "C": (113, 157, 108),+ "D": (215, 182, 143),+ "E": (57, 26, 252),+ "F": (225, 135, 33),+ }+@convert_parameter_to_seconds(["duration"])def __init__(self, bitmap_frames, *, fps=None, duration=None, color_dict=None, is_mask=False@@ -1390,21 +1403,7 @@ class BitmapClip(VideoClip):"""assert fps is not None or duration is not None- if color_dict:- self.color_dict = color_dict- else:- self.color_dict = {- "R": (255, 0, 0),- "G": (0, 255, 0),- "B": (0, 0, 255),- "O": (0, 0, 0),- "W": (255, 255, 255),- "A": (89, 225, 62),- "C": (113, 157, 108),- "D": (215, 182, 143),- "E": (57, 26, 252),- "F": (225, 135, 33),- }+ self.color_dict = color_dict if color_dict else self.DEFAULT_COLOR_DICTframe_list = []for input_frame in bitmap_frames:diff --git a/tests/test_fx.py b/tests/test_fx.pyindex 40f6fd53..04b130b3 100644--- a/tests/test_fx.py+++ b/tests/test_fx.py@@ -1,4 +1,5 @@import os+import randomimport numpy as npimport pytest@@ -42,10 +43,73 @@ def test_accel_decel():def test_blackwhite():- # TODO update to use BitmapClip- clip = get_test_video()- clip1 = blackwhite(clip)- clip1.write_videofile(os.path.join(TMP_DIR, "blackwhite1.webm"))+ # Create black/white spectrum ``bw_color_dict`` to compare against it.+ # Colors after ``blackwhite`` FX must be inside this dictionary+ # Note: black/white spectrum is made of colors with same numbers+ # [(0, 0, 0), (1, 1, 1), (2, 2, 2)...]+ bw_color_dict = {}+ for num in range(0, 256):+ bw_color_dict[chr(num + 255)] = (num, num, num)+ color_dict = bw_color_dict.copy()+ # update dictionary with default BitmapClip color_dict values+ color_dict.update(BitmapClip.DEFAULT_COLOR_DICT)++ # add row with random colors in b/w spectrum+ random_row = ""+ for num in range(512, 515):+ # use unique unicode representation for each color+ char = chr(num)+ random_row += char++ # random colors in the b/w spectrum+ color_dict[char] = tuple(random.randint(0, 255) for i in range(3))++ # clip converted below to black/white+ clip = BitmapClip([["RGB", random_row]], color_dict=color_dict, fps=1)++ # for each possible ``preserve_luminosity`` boolean argument value+ for preserve_luminosity in [True, False]:+ # default argument (``RGB=None``)+ clip_bw = blackwhite(clip, preserve_luminosity=preserve_luminosity)++ bitmap = clip_bw.to_bitmap()+ assert bitmap++ for i, row in enumerate(bitmap[0]):+ for char in row:+ # all characters returned by ``to_bitmap`` are in the b/w spectrum+ assert char in bw_color_dict++ if i == 0: # pure "RGB" colors are converted to [85, 85, 85]+ assert char == row[0] # so are equal++ # custom random ``RGB`` argument+ clip_bw_custom_rgb = blackwhite(+ clip,+ RGB=(random.randint(0, 255), 0, 0),+ preserve_luminosity=preserve_luminosity,+ )+ bitmap = clip_bw_custom_rgb.to_bitmap()+ for i, row in enumerate(bitmap[0]):+ for i2, char in enumerate(row):+ # all characters returned by ``to_bitmap`` are in the b/w spectrum+ assert char in bw_color_dict++ # for clip "RGB" row, two latest converted colors are equal+ if i == 0 and i2 > 0:+ assert char == row[1] and char == row[2]++ # ``RGB="CRT_phosphor"`` argument+ clip_bw_crt_phosphor = blackwhite(+ clip, RGB="CRT_phosphor", preserve_luminosity=preserve_luminosity+ )+ bitmap = clip_bw_crt_phosphor.to_bitmap()+ assert bitmap+ for row in bitmap[0]:+ for char in row:+ # all characters returned by ``to_bitmap`` are in the b/w spectrum+ assert char in bw_color_dict+close_all_clips(locals())