Skip to content

Video editing with Python

Add 'mask_and' FX tests (#1486)

Álvaro Mondéjar190fac69ac6f
diff --git a/moviepy/video/fx/mask_and.py b/moviepy/video/fx/mask_and.py
index 05ffb583..8e61eb78 100644
--- a/moviepy/video/fx/mask_and.py
+++ b/moviepy/video/fx/mask_and.py
@@ -4,12 +4,26 @@ from moviepy.video.VideoClip import ImageClip
def mask_and(clip, other_clip):
- """Returns the logical 'and' (min) between two masks.
+ """Returns the logical 'and' (minimum 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_and, mask) # black
+ >>> masked_clip.get_frame(0)
+ [[[0 0 0]]]
"""
- # To ensure that 'or' of two ImageClips will be an ImageClip.
+ # to ensure that 'and' 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 5779192f..65b0b40f 100644
--- a/tests/test_fx.py
+++ b/tests/test_fx.py
@@ -4,7 +4,14 @@ import random
import numpy as np
import pytest
-from moviepy import AudioClip, AudioFileClip, BitmapClip, ColorClip, VideoFileClip
+from moviepy import (
+ AudioClip,
+ AudioFileClip,
+ BitmapClip,
+ ColorClip,
+ VideoClip,
+ VideoFileClip,
+)
from moviepy.audio.fx import audio_normalize, multiply_stereo_volume, multiply_volume
from moviepy.utils import close_all_clips
from moviepy.video.fx import (
@@ -20,6 +27,7 @@ from moviepy.video.fx import (
lum_contrast,
make_loopable,
margin,
+ mask_and,
mirror_x,
mirror_y,
multiply_color,
@@ -442,8 +450,57 @@ def test_margin():
assert target == clip3
-def test_mask_and():
- pass
[email protected]("image_from", ("np.ndarray", "ImageClip"))
[email protected]("duration", (None, "random"))
+ ("color", "mask_color", "expected_color"),
+ (
+ (
+ (0, 0, 0),
+ (255, 255, 255),
+ (0, 0, 0),
+ ),
+ (
+ (255, 0, 0),
+ (0, 0, 255),
+ (0, 0, 0),
+ ),
+ (
+ (255, 255, 255),
+ (0, 10, 20),
+ (0, 10, 20),
+ ),
+ (
+ (10, 10, 10),
+ (20, 0, 20),
+ (10, 0, 10),
+ ),
+ ),
+)
+def test_mask_and(image_from, duration, color, mask_color, expected_color):
+ """Checks ``mask_and`` 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_and(
+ 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_and(clip, mask_clip)
+
+ assert np.array_equal(masked_clip.get_frame(0)[0][0], np.array(expected_color))
def test_mask_color():