Skip to content

Video editing with Python

Add 'mask_or' FX tests (#1485)

Álvaro Mondéjar428b46c2a570
diff --git a/moviepy/video/fx/mask_or.py b/moviepy/video/fx/mask_or.py
index e3670bd3..b435ff65 100644
--- a/moviepy/video/fx/mask_or.py
+++ b/moviepy/video/fx/mask_or.py
@@ -4,12 +4,26 @@ from moviepy.video.VideoClip import ImageClip
def mask_or(clip, other_clip):
- """Returns the logical 'or' (max) between two masks.
+ """Returns the logical 'or' (maximum pixel color values) between two masks.
- ``other_clip`` can be a mask clip or a picture (np.array).
- The result has the duration of 'clip' (if it has any).
+ The result has the duration of the clip to which has been applied, if it has any.
+
+ Parameters
+ ----------
+
+ other_clip ImageClip or np.ndarray
+ Clip used to mask the original clip.
+
+ Examples
+ --------
+
+ >>> clip = ColorClip(color=(255, 0, 0), size=(1, 1)) # red
+ >>> mask = ColorClip(color=(0, 255, 0), size=(1, 1)) # green
+ >>> masked_clip = clip.fx(mask_or, mask) # yellow
+ >>> masked_clip.get_frame(0)
+ [[[255 255 0]]]
"""
- # To ensure that 'or' of two ImageClips will be an ImageClip.
+ # to ensure that 'or' of two ImageClips will be an ImageClip
if isinstance(other_clip, ImageClip):
other_clip = other_clip.img
diff --git a/tests/test_fx.py b/tests/test_fx.py
index 65b0b40f..4e35bcd0 100644
--- a/tests/test_fx.py
+++ b/tests/test_fx.py
@@ -28,6 +28,7 @@ from moviepy.video.fx import (
make_loopable,
margin,
mask_and,
+ mask_or,
mirror_x,
mirror_y,
multiply_color,
@@ -507,8 +508,57 @@ def test_mask_color():
pass
-def test_mask_or():
- pass
[email protected]("image_from", ("np.ndarray", "ImageClip"))
[email protected]("duration", (None, "random"))
+ ("color", "mask_color", "expected_color"),
+ (
+ (
+ (0, 0, 0),
+ (255, 255, 255),
+ (255, 255, 255),
+ ),
+ (
+ (255, 0, 0),
+ (0, 0, 255),
+ (255, 0, 255),
+ ),
+ (
+ (255, 255, 255),
+ (0, 10, 20),
+ (255, 255, 255),
+ ),
+ (
+ (10, 10, 10),
+ (20, 0, 20),
+ (20, 10, 20),
+ ),
+ ),
+)
+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_mirror_x():