def test_mask_or(image_from, duration, color, mask_color, expected_color): """Checks ``mask_or`` FX behaviour.""" clip_size = tuple(random.randint(3, 10) for i in range(2)) if duration == "random": duration = round(random.uniform(0, 0.5), 2) # test ImageClip and np.ndarray types as mask argument clip = ColorClip(color=color, size=clip_size).with_duration(duration) mask_clip = ColorClip(color=mask_color, size=clip.size) masked_clip = mask_or( clip, mask_clip if image_from == "ImageClip" else mask_clip.get_frame(0)) assert masked_clip.duration == clip.duration assert np.array_equal( masked_clip.get_frame(0)[0][0], np.array(expected_color)) # test VideoClip as mask argument color_frame, mask_color_frame = (np.array([[color]]), np.array([[mask_color]])) clip = VideoClip(lambda t: color_frame).with_duration(duration) mask_clip = VideoClip(lambda t: mask_color_frame).with_duration(duration) masked_clip = mask_or(clip, mask_clip) assert np.array_equal( masked_clip.get_frame(0)[0][0], np.array(expected_color))
def test_rotate_mask(): # Prior to https://github.com/Zulko/moviepy/pull/1399 # all the pixels of the resulting video were 0 clip = (ColorClip(color=0.5, size=(1, 1), is_mask=True).with_fps(1).with_duration(1).fx( rotate, 45)) assert clip.get_frame(0)[1][1] != 0
def test_margin(ClipClass, margin_size, margins, color, expected_result): if ClipClass is BitmapClip: clip = BitmapClip([["RRR", "RRR"], ["RRR", "RRR"]], fps=1) else: clip = ColorClip(color=(255, 0, 0), size=(3, 2), duration=2).with_fps(1) # if None, set default argument values if color is None: color = (0, 0, 0) if margins is None: margins = [0, 0, 0, 0] left, right, top, bottom = margins new_clip = margin( clip, margin_size=margin_size, left=left, right=right, top=top, bottom=bottom, color=color, ) assert new_clip == BitmapClip(expected_result, fps=1)
def test_resize(library, apply_to_mask, size, duration, new_size, height, width, monkeypatch): """Checks ``resize`` FX behaviours using all argument and third party implementation combinations. """ # mock implementation resize_fx_mod = sys.modules[resize.__module__] resizer_func, error_msgs = { "PIL": resize_fx_mod._get_PIL_resizer, "cv2": resize_fx_mod._get_cv2_resizer, "scipy": resize_fx_mod._get_scipy_resizer, }[library]() # if function is not available, skip test for implementation if error_msgs: pytest.skip(error_msgs[0].split(" (")[0]) monkeypatch.setattr(resize_fx_mod, "resizer", resizer_func) # build expected sizes (using `width` or `height` arguments will be proportional # to original size) if new_size: if hasattr(new_size, "__call__"): # function expected_new_sizes = [new_size(t) for t in range(duration)] elif isinstance(new_size, numbers.Number): # scaling factor expected_new_sizes = [[ int(size[0] * new_size), int(size[1] * new_size) ]] else: # tuple or list expected_new_sizes = [new_size] elif height: if hasattr(height, "__call__"): expected_new_sizes = [] for t in range(duration): new_height = height(t) expected_new_sizes.append( [int(size[0] * new_height / size[1]), new_height]) else: expected_new_sizes = [[size[0] * height / size[1], height]] elif width: if hasattr(width, "__call__"): expected_new_sizes = [] for t in range(duration): new_width = width(t) expected_new_sizes.append( [new_width, int(size[1] * new_width / size[0])]) else: expected_new_sizes = [[width, size[1] * width / size[0]]] else: expected_new_sizes = None clip = ColorClip(size=size, color=(0, 0, 0), duration=duration) clip.fps = 1 mask = ColorClip(size=size, color=0, is_mask=True) clip = clip.with_mask(mask) # any resizing argument passed, raises `ValueError` if expected_new_sizes is None: with pytest.raises(ValueError): resized_clip = clip.resize( new_size=new_size, height=height, width=width, apply_to_mask=apply_to_mask, ) resized_clip = clip expected_new_sizes = [size] else: resized_clip = clip.resize(new_size=new_size, height=height, width=width, apply_to_mask=apply_to_mask) # assert new size for each frame for t in range(duration): expected_width = expected_new_sizes[t][0] expected_height = expected_new_sizes[t][1] clip_frame = resized_clip.get_frame(t) assert len(clip_frame[0]) == expected_width assert len(clip_frame) == expected_height mask_frame = resized_clip.mask.get_frame(t) if apply_to_mask: assert len(mask_frame[0]) == expected_width assert len(mask_frame) == expected_height
def test_rotate_nonstandard_angles(util): # Test rotate with color clip clip = ColorClip([600, 400], [150, 250, 100]).with_duration(1).with_fps(5) clip = rotate(clip, 20) clip.write_videofile(os.path.join(util.TMP_DIR, "color_rotate.webm"))