Video editing with Python
Optimise clip compositing using PIL (replacement for #1157) (#1342)
Tom Burrowsfe5c782f1321
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.pyindex a034088f..9629f18e 100644--- a/moviepy/video/VideoClip.py+++ b/moviepy/video/VideoClip.py@@ -11,7 +11,6 @@ import tempfileimport numpy as npimport proglogfrom imageio import imread, imsave-from moviepy.Clip import Clipfrom moviepy.config import IMAGEMAGICK_BINARYfrom moviepy.decorators import (@@ -23,7 +22,6 @@ from moviepy.decorators import (outplace,requires_duration,use_clip_fps_by_default,- convert_path_to_string,)from moviepy.tools import extensions_dict, find_extension, subprocess_callfrom moviepy.video.io.ffmpeg_writer import ffmpeg_write_video@@ -33,6 +31,7 @@ from moviepy.video.io.gif_writers import (write_gif_with_tempfiles,)from moviepy.video.tools.drawing import blit+from PIL import Imageclass VideoClip(Clip):@@ -556,11 +555,11 @@ class VideoClip(Clip):>>> new_clip = clip.subapply(lambda c:c.speedx(0.5) , 3,6)"""- left = self.subclip(0, start_time) if start_time else None+ left = None if (start_time == 0) else self.subclip(0, start_time)center = self.subclip(start_time, end_time).fx(fx, **kwargs)- right = self.subclip(start_time=end_time) if end_time else None+ right = None if (end_time is None) else self.subclip(start_time=end_time)- clips = [clip for clip in (left, center, right) if clip]+ clips = [clip for clip in [left, center, right] if clip is not None]# beurk, have to find other solutionfrom moviepy.video.compositing.concatenate import concatenate_videoclips@@ -603,25 +602,36 @@ class VideoClip(Clip):on the given `picture`, the position of the clip being givenby the clip's ``pos`` attribute. Meant for compositing."""- hf, wf = framesize = picture.shape[:2]-- if self.is_mask and picture.max():- return np.minimum(1, picture + self.blit_on(np.zeros(framesize), t))+ hf, wf = picture.sizect = t - self.start # clip time# GET IMAGE AND MASK IF ANY+ img = self.get_frame(ct).astype("uint8")+ im_img = Image.fromarray(img)- img = self.get_frame(ct)- mask = self.mask.get_frame(ct) if self.mask else None+ if self.mask is not None:+ mask = self.mask.get_frame(ct).astype("uint8")+ im_mask = Image.fromarray(255 * mask).convert("L")- if mask is not None and (- (img.shape[0] != mask.shape[0]) or (img.shape[1] != mask.shape[1])- ):- img = self.fill_array(img, mask.shape)+ if im_img.size != im_mask.size:+ bg_size = (+ max(im_img.size[0], im_mask.size[0]),+ max(im_img.size[1], im_mask.size[1]),+ )- hi, wi = img.shape[:2]+ im_img_bg = Image.new("RGB", bg_size, "black")+ im_img_bg.paste(im_img, (0, 0))+ im_mask_bg = Image.new("L", bg_size, 0)+ im_mask_bg.paste(im_mask, (0, 0))++ im_img, im_mask = im_img_bg, im_mask_bg++ else:+ im_mask = None++ hi, wi = im_img.size# SET POSITIONpos = self.pos(ct)@@ -652,8 +662,7 @@ class VideoClip(Clip):pos[1] = D[pos[1]]pos = map(int, pos)-- return blit(img, picture, pos, mask=mask, is_mask=self.is_mask)+ return blit(im_img, picture, pos, mask=im_mask)def add_mask(self):"""Add a mask VideoClip to the VideoClip.diff --git a/moviepy/video/compositing/CompositeVideoClip.py b/moviepy/video/compositing/CompositeVideoClip.pyindex 58cee0eb..b73d8e11 100644--- a/moviepy/video/compositing/CompositeVideoClip.py+++ b/moviepy/video/compositing/CompositeVideoClip.py@@ -2,6 +2,7 @@ import numpy as npfrom moviepy.audio.AudioClip import CompositeAudioClipfrom moviepy.video.VideoClip import ColorClip, VideoClip+from PIL import Image# CompositeVideoClip@@ -9,7 +10,6 @@ from moviepy.video.VideoClip import ColorClip, VideoClipclass CompositeVideoClip(VideoClip):"""-A VideoClip made of other videoclips displayed together. This is thebase class for most compositions.@@ -116,16 +116,21 @@ class CompositeVideoClip(VideoClip):maskclips, self.size, is_mask=True, bg_color=0.0)- def make_frame(t):- """The clips playing at time `t` are blitted over one- another."""+ def make_frame(self, t):+ """The clips playing at time `t` are blitted over one another."""++ frame = self.bg.get_frame(t).astype("uint8")+ im = Image.fromarray(frame)- frame = self.bg.get_frame(t)- for clip in self.playing_clips(t):- frame = clip.blit_on(frame, t)- return frame+ if self.bg.mask is not None:+ frame_mask = self.bg.mask.get_frame(t)+ im_mask = Image.fromarray(255 * frame_mask).convert("L")+ im = im.putalpha(im_mask)- self.make_frame = make_frame+ for clip in self.playing_clips(t):+ im = clip.blit_on(im, t)++ return np.array(im)def playing_clips(self, t=0):"""Returns a list of the clips in the composite clips that are@@ -144,7 +149,6 @@ class CompositeVideoClip(VideoClip):def clips_array(array, rows_widths=None, cols_widths=None, bg_color=None):-"""rows_widthsdiff --git a/moviepy/video/tools/drawing.py b/moviepy/video/tools/drawing.pyindex 18ef7fa4..d86ff158 100644--- a/moviepy/video/tools/drawing.py+++ b/moviepy/video/tools/drawing.py@@ -6,46 +6,20 @@ methods that are difficult to do with the existing Python libraries.import numpy as np-def blit(im1, im2, pos=None, mask=None, is_mask=False):+def blit(im1, im2, pos=None, mask=None):"""Blit an image over another.Blits ``im1`` on ``im2`` as position ``pos=(x,y)``, using the- ``mask`` if provided. If ``im1`` and ``im2`` are mask pictures- (2D float arrays) then ``is_mask`` must be ``True``.+ ``mask`` if provided."""if pos is None:pos = [0, 0]- # xp1,yp1,xp2,yp2 = blit area on im2- # x1,y1,x2,y2 = area of im1 to blit on im2xp, yp = pos- x1 = max(0, -xp)- y1 = max(0, -yp)- h1, w1 = im1.shape[:2]- h2, w2 = im2.shape[:2]- xp2 = min(w2, xp + w1)- yp2 = min(h2, yp + h1)- x2 = min(w1, w2 - xp)- y2 = min(h1, h2 - yp)xp1 = max(0, xp)yp1 = max(0, yp)- if (xp1 >= xp2) or (yp1 >= yp2):- return im2-- blitted = im1[y1:y2, x1:x2]-- new_im2 = +im2-- if mask is None:- new_im2[yp1:yp2, xp1:xp2] = blitted- else:- mask = mask[y1:y2, x1:x2]- if len(im1.shape) == 3:- mask = np.dstack(3 * [mask])- blit_region = new_im2[yp1:yp2, xp1:xp2]- new_im2[yp1:yp2, xp1:xp2] = 1.0 * mask * blitted + (1.0 - mask) * blit_region-- return new_im2.astype("uint8") if (not is_mask) else new_im2+ im2.paste(im1, (xp1, yp1), mask)+ return im2def color_gradient(