Skip to content

Video editing with Python

Fix default value bug in ColorClip (#1147)

Jonne Kaunisto5eb20a73c959
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index 667b4989..f182eb2d 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -1065,7 +1065,20 @@ class ColorClip(ImageClip):
def __init__(self, size, color=None, ismask=False, duration=None):
w, h = size
- shape = (h, w) if np.isscalar(color) else (h, w, len(color))
+
+ if ismask:
+ shape = (h, w)
+ if color is None:
+ color = 0
+ elif not np.isscalar(color):
+ raise Exception("Color has to be a scalar when mask is true")
+ else:
+ if color is None:
+ color = (0, 0, 0)
+ elif not hasattr(color, "__getitem__"):
+ raise Exception("Color has to contain RGB of the clip")
+ shape = (h, w, len(color))
+
super().__init__(
np.tile(color, w * h).reshape(shape), ismask=ismask, duration=duration
)
diff --git a/moviepy/video/compositing/CompositeVideoClip.py b/moviepy/video/compositing/CompositeVideoClip.py
index 0cef7ce0..6349befa 100644
--- a/moviepy/video/compositing/CompositeVideoClip.py
+++ b/moviepy/video/compositing/CompositeVideoClip.py
@@ -75,7 +75,7 @@ class CompositeVideoClip(VideoClip):
self.created_bg = False
else:
self.clips = clips
- self.bg = ColorClip(size, color=self.bg_color)
+ self.bg = ColorClip(size, color=self.bg_color, ismask=ismask)
self.created_bg = True
# compute duration
diff --git a/tests/test_VideoClip.py b/tests/test_VideoClip.py
index 69761607..e4196017 100644
--- a/tests/test_VideoClip.py
+++ b/tests/test_VideoClip.py
@@ -128,6 +128,18 @@ def test_oncolor():
location = os.path.join(TMP_DIR, "oncolor.mp4")
on_color_clip.write_videofile(location, fps=24)
assert os.path.isfile(location)
+
+ # test constructor with default arguements
+ clip = ColorClip(size=(100, 60), ismask=True)
+ clip = ColorClip(size=(100, 60), ismask=False)
+
+ # negative test
+ with pytest.raises(Exception):
+ clip = ColorClip(size=(100, 60), color=(255, 0, 0), ismask=True)
+
+ with pytest.raises(Exception):
+ clip = ColorClip(size=(100, 60), color=0.4, ismask=False)
+
close_all_clips(locals())