Skip to content

Video editing with Python

First working version of composition in pure numpy, without pillow, still a lot of clean up to do, and need to figure out how not to avoid computing masks twice

osaajani5de2848ce7ae
diff --git a/moviepy/Clip.py b/moviepy/Clip.py
index 2b636177..384e72e9 100644
--- a/moviepy/Clip.py
+++ b/moviepy/Clip.py
@@ -64,7 +64,7 @@ class Clip:
return _copy.copy(self)
@convert_parameter_to_seconds(["t"])
- def get_frame(self, t):
+ def get_frame(self, t) -> np.ndarray:
"""Gets a numpy array representing the RGB picture of the clip,
or (mono or stereo) value for a sound clip, at time ``t``.
@@ -73,6 +73,8 @@ class Clip:
t : float or tuple or str
Moment of the clip whose frame will be returned.
+
+
"""
# Coming soon: smart error handling for debugging at this point
if self.memoize:
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index 1b042d85..9b97048b 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -717,7 +717,7 @@ class VideoClip(Clip):
post_array = np.hstack((post_array, x_1))
return post_array
- def compose_on(self, background: Image.Image, t) -> Image.Image:
+ def compose_on(self, background: np.ndarray, t, background_mask: np.ndarray|None = None) -> np.ndarray:
"""Returns the result of the clip's frame at time `t` on top
on the given `picture`, the position of the clip being given
by the clip's ``pos`` attribute. Meant for compositing.
@@ -725,84 +725,168 @@ class VideoClip(Clip):
If the clip/backgrounds have transparency the transparency will
be accounted for.
- The return is a Pillow Image
+ The return is either a numpy array for image with no transparency or
+ a tuple (image, mask) for image with transparency
Parameters
----------
- backrgound (Image)
+ background: (np.ndarray)
The background image to apply current clip on top of
if the background image is transparent it must be given as a RGBA image
- t
+ t: (float)
The time of clip to apply on top of clip
- Return
+ background_mask: (np.ndarray|None), default None
+ The background mask to apply current clip on top of
+ if the background image is transparent it must be given as a mask
+ if None, the background is assumed to be fully opaque
+
+
+ Return
+ -------
+ np.ndarray | (np.ndarray, np.ndarray)
+ The background image with the clip applied on top of it
+ if the background image is transparent it will be returned as a RGBA image
+
"""
ct = t - self.start # clip time
# GET IMAGE AND MASK IF ANY
clip_frame = self.get_frame(ct).astype("uint8")
- clip_img = Image.fromarray(clip_frame)
+ background_height, background_width = background.shape[:2]
+ clip_height, clip_width = clip_frame.shape[:2]
+ clip_mask = None
if self.mask is not None:
- clip_mask = (self.mask.get_frame(ct) * 255).astype("uint8")
- clip_mask_img = Image.fromarray(clip_mask).convert("L")
+ # Clip mask normalize to 0 (fully transparent) to 1 (fully opaque)
+ clip_mask = self.mask.get_frame(ct)
# Resize clip_mask_img to match clip_img, always use top left corner
- if clip_mask_img.size != clip_img.size:
- mask_width, mask_height = clip_mask_img.size
- img_width, img_height = clip_img.size
-
- if mask_width > img_width or mask_height > img_height:
- # Crop mask if it is larger
- clip_mask_img = clip_mask_img.crop((0, 0, img_width, img_height))
- else:
- # Fill mask with 0 if it is smaller
- new_mask = Image.new("L", (img_width, img_height), 0)
- new_mask.paste(clip_mask_img, (0, 0))
- clip_mask_img = new_mask
-
- clip_img = clip_img.convert("RGBA")
- clip_img.putalpha(clip_mask_img)
+ if clip_frame.shape[:2] != clip_mask.shape[:2] :
+ mask_height, mask_width = clip_mask.shape[:2]
+
+ # If mask is larger, crop it
+ if mask_width > clip_width or mask_height > clip_height:
+ clip_mask = clip_mask[:clip_height, :clip_width]
+
+ # If mask is smaller, fill it with zeros
+ if mask_width < clip_width or mask_height < clip_height :
+ new_mask = np.zeros((clip_height, clip_width), dtype=clip_mask.dtype)
+ new_mask[:mask_height, :mask_width] = clip_mask
+ clip_mask = new_mask
# SET POSITION
pos = self.pos(ct)
- pos = compute_position(clip_img.size, background.size, pos, self.relative_pos)
-
- # If neither background nor clip have alpha layer (check if mode end
- # with A), we can juste use pillow paste
- if clip_img.mode[-1] != "A" and background.mode[-1] != "A":
- background.paste(clip_img, pos)
- return background
-
- # If background has no alpha layer, we can just paste img on top of it
- # this is far more efficient than alpha compositing
- if background.mode[-1] != "A":
- background.paste(clip_img, pos, mask=clip_img)
- return background
-
- # For images with transparency we must use pillow alpha composite
- # instead of a simple paste, because pillow paste dont work nicely
- # with alpha compositing
- # Only do this in last resort, as it is slower than a simple paste
- # and we dont want to use it if we dont have to
- if background.mode[-1] != "A":
- background = background.convert("RGBA")
-
- if clip_img.mode[-1] != "A":
- clip_img = clip_img.convert("RGBA")
-
- # We need both image to do the same size for alpha compositing in pillow
- # so we must start by making a fully transparent canvas of background's
- # size and paste our clip img into it in position pos, only then can we
- # composite this canvas on top of background
- # Its actually faster to make the canvas in numpy and then convert it to PIL
- # than to make it in PIL directly
- canvas_np = np.zeros((background.height, background.width, 4), dtype=np.uint8)
- canvas = Image.fromarray(canvas_np)
- canvas.paste(clip_img, pos)
- result = Image.alpha_composite(background, canvas)
- return result
+ x_start, y_start = compute_position((clip_width, clip_height), (background_width, background_height), pos, self.relative_pos)
+
+ # Clip destination coordinates in bg
+ y1_bg = max(y_start, 0)
+ y2_bg = min(y_start + clip_height, background_height)
+ x1_bg = max(x_start, 0)
+ x2_bg = min(x_start + clip_width, background_width)
+
+ # Corresponding source region in clip
+ y1_clip = max(-y_start, 0)
+ y2_clip = y1_clip + (y2_bg - y1_bg)
+ x1_clip = max(-x_start, 0)
+ x2_clip = x1_clip + (x2_bg - x1_bg)
+
+ # We will ignore any mask with no transparency so we only ever compute mask
+ # if really necessary
+ if background_mask is not None and np.min(background_mask) == 1 :
+ background_mask = None
+
+ if clip_mask is not None and np.min(clip_mask) == 1 :
+ clip_mask = None
+
+ # Copy the background to avoid modifying the original
+ b = background.copy()
+
+ if background_mask is not None:
+ bm = background_mask.copy()
+
+ # If neither background nor clip have a mask, we can just paste clip on top
+ if background_mask is None and clip_mask is None:
+ b[y1_bg:y2_bg, x1_bg:x2_bg] = clip_frame[y1_clip:y2_clip, x1_clip:x2_clip]
+ return (b, None)
+
+ # If clip has no alpha layer, we can compute final clip
+ # by replacing the background region with the clip region
+ # and fill the region mask with 1
+ if clip_mask is None:
+ b[y1_bg:y2_bg, x1_bg:x2_bg] = clip_frame[y1_clip:y2_clip, x1_clip:x2_clip]
+ bm[y1_bg:y2_bg, x1_bg:x2_bg] = np.ones((y2_bg - y1_bg, x2_bg - x1_bg), dtype=np.float32)
+ return (b, bm)
+
+
+ # If background has no alpha layer, we can compute final color
+ # accounting for transparency and return a result with no transparency
+ if background_mask is None :
+ # Extract regions, convert to float32 instead of letting numpy go for float64
+ frame = clip_frame[y1_clip:y2_clip, x1_clip:x2_clip].astype(np.float32)
+ bg = b[y1_bg:y2_bg, x1_bg:x2_bg].astype(np.float32)
+ alpha = clip_mask[y1_clip:y2_clip, x1_clip:x2_clip][..., None].astype(np.float32)
+
+ # To understand the math, think in "passing light" (see self.compose_mask)
+ # if first layer is 100% opacity with 100 red photons, 0 green, 0 blue [100, 0, 0]
+ # and second layer is 70% opacity, with 0 red photons, 0 green, 100 blue [0, 0, 100]
+ # then we got 100 red, but 70% are blocked, so we get 100 - 100*0.7 = 30 red
+ # and we also get 100 blue, of which only 70% are "emited", so we get 100*0.7 = 70 blue
+ # we get a final [30, 0, 70] fully opaque sheet
+ result = frame * alpha + bg * (1 - alpha)
+
+ # Finally we update the regions with result back to int
+ b[y1_bg:y2_bg, x1_bg:x2_bg] = result.astype(np.uint8)
+ return (b, None)
+
+
+ # For images with alpha layer on both clip and background
+ # we must compute both new color and new mask
+ # Again to understand the math, consider the following to calculate the final mask
+ # Note :
+ # Thinking in transparency is hard, as we tend to think
+ # that 50% opaque + 40% opaque = 90% opacity, when it really its 70%
+ # It's a lot easier to think in terms of "passing light"
+ # Consider I emit 100 photons, and my first layer is 50% opaque, meaning it
+ # will "stop" 50% of the photons, I'll have 50 photons left
+ # now my second layer is blocking 40% of thoses 50 photons left
+ # blocking 50 * 0.4 = 20 photons, and leaving me with only 30 photons
+ # So, by adding two layer of 50% and 40% opacity my finaly opacity is only
+ # of (100-30)*100 = 70% opacity !
+ # the formula for final alpha is for B over A is : opacity B + opacity A * (1 - opacity B)
+
+ # We can understand the formula by thinking that a color with X% opacity will only emit color * X photons :
+ # B will emit 100 photons multiply by B opacity, so if opacity is 25%, 25 photons will pass
+ # It will also let pass the photons emitted by A, (100 * A opacity), for A = 50, 50 photons
+ # but of thoses 50, 25% will be blocked, so 50 - (50 * 0.25) = 50 * 0.75 = 37.5
+ # so at the end we get 25 + 37.5 = 62.5 photons, so the same as a one layer opacity of 62.5%
+ # so, how many photon B will let through + (how many photon A will let through * how many of thouse B will let through)
+ # again we get aB + aA * (1 - aB)
+ # at the end we must divide color by resulting alpha to keep true colors
+
+ # Extract regions, convert to float32 instead of letting numpy go for float64
+ frame = clip_frame[y1_clip:y2_clip, x1_clip:x2_clip].astype(np.float32)
+ bg = b[y1_bg:y2_bg, x1_bg:x2_bg].astype(np.float32)
+ alpha_clip = clip_mask[y1_clip:y2_clip, x1_clip:x2_clip].astype(np.float32)
+ alpha_bg = background_mask[y1_bg:y2_bg, x1_bg:x2_bg].astype(np.float32)
+
+ # Ensure alpha channels have the right shape for broadcasting
+ if alpha_clip.ndim == 2:
+ alpha_clip = alpha_clip[..., None]
+ if alpha_bg.ndim == 2:
+ alpha_bg = alpha_bg[..., None]
+
+ final_alpha = alpha_clip + alpha_bg * (1 - alpha_clip)
+ safe_alpha = np.where(final_alpha==0, 1.0, final_alpha)
+ result = (frame * alpha_clip + bg * alpha_bg * (1 - alpha_clip)) / safe_alpha
+
+ b[y1_bg:y2_bg, x1_bg:x2_bg] = result.astype(np.uint8)
+
+ bm[y1_bg:y2_bg, x1_bg:x2_bg] = final_alpha.squeeze()
+ return (b, bm)
+
+
def compose_mask(self, background_mask: np.ndarray, t: float) -> np.ndarray:
"""Returns the result of the clip's mask at time `t` composited
@@ -828,26 +912,22 @@ class VideoClip(Clip):
# SET POSITION
pos = self.pos(ct)
- pos = compute_position((clip_w, clip_h), (bg_w, bg_h), pos, self.relative_pos)
-
- # ALPHA COMPOSITING
- # Determine the base_mask region to merge size
- x_start = int(max(pos[0], 0)) # Dont go under 0 left
- x_end = int(min(pos[0] + clip_w, bg_w)) # Dont go over base_mask width
- y_start = int(max(pos[1], 0)) # Dont go under 0 top
- y_end = int(min(pos[1] + clip_h, bg_h)) # Dont go over base_mask height
-
- # Determine the clip_mask region to overlapp
- # Dont go under 0 for horizontal, if we have negative margin of X px start at X
- # And dont go over clip width
- clip_x_start = int(max(0, -pos[0]))
- clip_x_end = int(clip_x_start + min((x_end - x_start), (clip_w - clip_x_start)))
- # same for vertical
- clip_y_start = int(max(0, -pos[1]))
- clip_y_end = int(clip_y_start + min((y_end - y_start), (clip_h - clip_y_start)))
+ x_start, y_start = compute_position((clip_w, clip_h), (bg_w, bg_h), pos, self.relative_pos)
+
+ # Clip destination coordinates in bg
+ y1_bg = max(y_start, 0)
+ y2_bg = min(y_start + clip_h, bg_h)
+ x1_bg = max(x_start, 0)
+ x2_bg = min(x_start + clip_w, bg_w)
+
+ # Corresponding source region in clip
+ y1_clip = max(-y_start, 0)
+ y2_clip = y1_clip + (y2_bg - y1_bg)
+ x1_clip = max(-x_start, 0)
+ x2_clip = x1_clip + (x2_bg - x1_bg)
# Blend the overlapping regions
- # The calculus is base_opacity + clip_opacity * (1 - base_opacity)
+ # The calculus is clip_opacity + bg_opacity * (1 - base_opacity)
# this ensure that masks are drawn in the right order and
# the contribution of each mask is proportional to their transparency
#
@@ -861,13 +941,15 @@ class VideoClip(Clip):
# blocking 50 * 0.4 = 20 photons, and leaving me with only 30 photons
# So, by adding two layer of 50% and 40% opacity my finaly opacity is only
# of (100-30)*100 = 70% opacity !
- background_mask[y_start:y_end, x_start:x_end] = background_mask[
- y_start:y_end, x_start:x_end
- ] + clip_mask[clip_y_start:clip_y_end, clip_x_start:clip_x_end] * (
- 1 - background_mask[y_start:y_end, x_start:x_end]
+
+ # Copy the background mask to avoid modifying the original
+ b = background_mask.copy()
+
+ b[y1_bg:y2_bg, x1_bg:x2_bg] = clip_mask[y1_clip:y2_clip, x1_clip:x2_clip] + background_mask[y1_bg:y2_bg, x1_bg:x2_bg] * (
+ 1 - clip_mask[y1_clip:y2_clip, x1_clip:x2_clip]
)
- return background_mask
+ return b
def with_background_color(self, size=None, color=(0, 0, 0), pos=None, opacity=None):
"""Place the clip on a colored background.
diff --git a/moviepy/video/compositing/CompositeVideoClip.py b/moviepy/video/compositing/CompositeVideoClip.py
index ef753ef4..028f7917 100644
--- a/moviepy/video/compositing/CompositeVideoClip.py
+++ b/moviepy/video/compositing/CompositeVideoClip.py
@@ -123,7 +123,7 @@ class CompositeVideoClip(VideoClip):
if use_bgclip and self.bg.mask:
maskclips = [self.bg.mask] + maskclips
-
+
self.mask = CompositeVideoClip(
maskclips, self.size, is_mask=True, bg_color=0.0
)
@@ -139,39 +139,36 @@ class CompositeVideoClip(VideoClip):
return mask
- # Try doing clip merging with pillow
+ # Try doing clip merging without pillow
bg_t = t - self.bg.start
bg_frame = self.bg.get_frame(bg_t).astype("uint8")
- bg_img = Image.fromarray(bg_frame)
+ clip_height, clip_width = bg_frame.shape[:2]
if self.bg.mask:
bgm_t = t - self.bg.mask.start
- bg_mask = (self.bg.mask.get_frame(bgm_t) * 255).astype("uint8")
- bg_mask_img = Image.fromarray(bg_mask).convert("L")
+ bg_mask = self.bg.mask.get_frame(bgm_t)
- # Resize bg_mask_img to match bg_img, always use top left corner
- if bg_mask_img.size != bg_img.size:
- mask_width, mask_height = bg_mask_img.size
- img_width, img_height = bg_img.size
+ # Resize bg_mask to match bg_frame, always use top left corner
+ if bg_frame.shape[:2] != bg_mask.shape[:2] :
+ mask_height, mask_width = bg_mask.shape[:2]
- if mask_width > img_width or mask_height > img_height:
- bg_mask_img = bg_mask_img.crop((0, 0, img_width, img_height))
- else:
- new_mask = Image.new("L", (img_width, img_height), 0)
- new_mask.paste(bg_mask_img, (0, 0))
- bg_mask_img = new_mask
+ # If mask is larger, crop it
+ if mask_width > clip_width or mask_height > clip_height:
+ bg_mask = bg_mask[:clip_height, :clip_width]
- bg_img = bg_img.convert("RGBA")
- bg_img.putalpha(bg_mask_img)
+ # If mask is smaller, fill it with zeros
+ if mask_width < clip_width or mask_height < clip_height :
+ new_mask = np.zeros((clip_height, clip_width), dtype=bg_mask.dtype)
+ new_mask[:mask_height, :mask_width] = bg_mask
+ bg_mask = new_mask
# For each clip apply on top of current img
- current_img = bg_img
+ current_img = bg_frame
+ current_mask = bg_mask if self.bg.mask else None
for clip in self.playing_clips(t):
- current_img = clip.compose_on(current_img, t)
+ current_img, current_mask = clip.compose_on(current_img, t, current_mask)
- # Turn Pillow image into a numpy array
- # force contigous for perfs
- frame = np.ascontiguousarray(current_img)
+ frame = current_img
# If frame have transparency, remove it
# our mask will take care of it during rendering
diff --git a/tests/test_compositing.py b/tests/test_compositing.py
index 3ba11391..bbefe680 100644
--- a/tests/test_compositing.py
+++ b/tests/test_compositing.py
@@ -163,19 +163,19 @@ def test_compositing_with_transparency_colors(util):
# For possible rounding errors
color1 = frame[50, 10]
opacity1 = mask[50, 10]
- assert np.allclose(color1, [255, 0, 0], rtol=0.01)
+ assert np.allclose(color1, [255, 0, 0], rtol=1)
assert abs(opacity1 - 0.3) < 0.01
# With 2 layers
color2 = frame[50, 30]
opacity2 = mask[50, 30]
- assert np.allclose(color2, [105, 150, 0], rtol=0.01)
+ assert np.allclose(color2, [105, 150, 0], rtol=1)
assert abs(opacity2 - 0.51) < 0.01
# With 3 layers
color3 = frame[50, 50]
opacity3 = mask[50, 50]
- assert np.allclose(color3, [57, 82, 116], rtol=0.01)
+ assert np.allclose(color3, [56, 81, 116], rtol=1)
assert abs(opacity3 - 0.657) < 0.01