Skip to content

Video editing with Python

VideoClip blitting: fix truncation bug to enable fractional opacity (#1552)

Christoph Reichenbach495948e2db65
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 478b3b5f..e1097797 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -50,6 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed `fps` not defined in `CompositeAudioClip` at initialization [\#1462](https://github.com/Zulko/moviepy/pull/1462)
- Fixed `clip.preview()` crashing at exit when running inside Jupyter Notebook in Windows [\#1537](https://github.com/Zulko/moviepy/pull/1537)
- Fixed rotate FX not being applied to mask images [\#1399](https://github.com/Zulko/moviepy/pull/1399)
+- Fixed opacity error blitting VideoClips [\#1552](https://github.com/Zulko/moviepy/pull/1552)
## [v2.0.0.dev2](https://github.com/zulko/moviepy/tree/v2.0.0.dev2) (2020-10-05)
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index ac23a487..229d459d 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -636,8 +636,8 @@ class VideoClip(Clip):
im_img = Image.fromarray(img)
if self.mask is not None:
- mask = self.mask.get_frame(ct).astype("uint8")
- im_mask = Image.fromarray(255 * mask).convert("L")
+ mask = (self.mask.get_frame(ct) * 255).astype("uint8")
+ im_mask = Image.fromarray(mask).convert("L")
if im_img.size != im_mask.size:
bg_size = (
diff --git a/moviepy/video/tools/segmenting.py b/moviepy/video/tools/segmenting.py
index 24f2ee28..0f37fc63 100644
--- a/moviepy/video/tools/segmenting.py
+++ b/moviepy/video/tools/segmenting.py
@@ -32,7 +32,7 @@ def find_objects(clip, size_threshold=500, preview=False):
letters = []
for i, (sy, sx) in indexed_slices:
- """ crop each letter separately """
+ # crop each letter separately
sy = slice(sy.start - 1, sy.stop + 1)
sx = slice(sx.start - 1, sx.stop + 1)
letter = image[sy, sx]
diff --git a/setup.cfg b/setup.cfg
index 980f1be4..fd41e180 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -29,7 +29,7 @@ per-file-ignores =
# the version file doesn't need module level docstring
moviepy/version.py: D100
# tests doesn't require docstring (although is recommended)
- tests/*.py: D103
+ tests/*.py: D101,D102,D103
# allow 'from moviepy import *' in examples
examples/*.py: F403, F405
docstring-convention = numpy
diff --git a/tests/test_compositing.py b/tests/test_compositing.py
index 02b9ca66..c310c605 100644
--- a/tests/test_compositing.py
+++ b/tests/test_compositing.py
@@ -5,14 +5,37 @@ import os
import pytest
from moviepy.utils import close_all_clips
-from moviepy.video.compositing.CompositeVideoClip import clips_array
+from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip, clips_array
from moviepy.video.compositing.concatenate import concatenate_videoclips
from moviepy.video.fx.resize import resize
+from moviepy.video.io.VideoFileClip import VideoFileClip
from moviepy.video.VideoClip import BitmapClip, ColorClip
from tests.test_helper import TMP_DIR
+class ClipPixelTest:
+ ALLOWABLE_COLOR_VARIATION = 3 # from 0-767: how much mismatch do we allow
+
+ def __init__(self, clip):
+ self.clip = clip
+
+ def expect_color_at(self, ts, expected, xy=[0, 0]):
+ frame = self.clip.make_frame(ts)
+ r, g, b = expected
+ actual = frame[xy[1]][xy[0]]
+ diff = abs(actual[0] - r) + abs(actual[1] - g) + abs(actual[2] - b)
+
+ mismatch = diff > ClipPixelTest.ALLOWABLE_COLOR_VARIATION
+ assert (
+ not mismatch
+ ), "Expected (%02x,%02x,%02x) but got (%02x,%02x,%02x) at timestamp %s" % (
+ *expected,
+ *actual,
+ ts,
+ )
+
+
def test_clips_array():
red = ColorClip((1024, 800), color=(255, 0, 0))
green = ColorClip((1024, 800), color=(0, 255, 0))
@@ -67,3 +90,26 @@ def test_concatenate_floating_point():
concat = concatenate_videoclips([clip])
concat.write_videofile(os.path.join(TMP_DIR, "concat.mp4"), preset="ultrafast")
close_all_clips(locals())
+
+
+def test_blit_with_opacity():
+ # bitmap.mp4 has one second R, one second G, one second B
+ clip1 = VideoFileClip("media/bitmap.mp4")
+ # overlay same clip, shifted by 1 second, at half opacity
+ clip2 = (
+ VideoFileClip("media/bitmap.mp4")
+ .subclip(1, 2)
+ .with_start(0)
+ .with_end(2)
+ .with_opacity(0.5)
+ )
+ composite = CompositeVideoClip([clip1, clip2])
+ bt = ClipPixelTest(composite)
+
+ bt.expect_color_at(0.5, (0x7F, 0x7F, 0x00))
+ bt.expect_color_at(1.5, (0x00, 0x7F, 0x7F))
+ bt.expect_color_at(2.5, (0x00, 0x00, 0xFF))
+
+
+if __name__ == "__main__":
+ pytest.main()