Skip to content

Video editing with Python

fixed bug in rotation, buggy typos in ImageClip fl_image and fl_time

Zulko477246171bc0
diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py
index 25942ab8..4b6dd710 100644
--- a/moviepy/video/VideoClip.py
+++ b/moviepy/video/VideoClip.py
@@ -747,13 +747,15 @@ class ImageClip(VideoClip):
elif ismask:
img = 1.0 * img[:, :, 0] / 255
- # if the image was just a 2D mask, it should arrive here unchanged
+ # if the image was just a 2D mask, it should arrive here
+ # unchanged
self.get_frame = lambda t: img
self.size = img.shape[:2][::-1]
self.img = img
+
def fl(self, fl, apply_to=[], keep_duration=True):
""" General transformation filter.
@@ -769,6 +771,7 @@ class ImageClip(VideoClip):
return newclip
+
@outplace
def fl_image(self, image_func, apply_to= []):
""" Image-transformation filter.
@@ -787,10 +790,11 @@ class ImageClip(VideoClip):
if hasattr(self, attr):
a = getattr(self, attr)
if a != None:
- new_a = a.fl(image_func)
+ new_a = a.fl_image(image_func)
setattr(newclip, attr, new_a)
+
@outplace
def fl_time(self, time_func, apply_to =['mask', 'audio']):
""" Time-transformation filter.
@@ -806,9 +810,9 @@ class ImageClip(VideoClip):
if hasattr(self, attr):
a = getattr(self, attr)
if a != None:
- new_a = a.fl_image(time_func)
+ new_a = a.fl_time(time_func)
setattr(self, attr, new_a)
-
+
class ColorClip(ImageClip):
diff --git a/moviepy/video/fx/rotation.py b/moviepy/video/fx/rotation.py
index 9a92b9e1..e05ba64c 100644
--- a/moviepy/video/fx/rotation.py
+++ b/moviepy/video/fx/rotation.py
@@ -1,17 +1,24 @@
-
-
+import numpy as np
def rotation(clip, angle, apply_to_mask=True, unit='deg'):
+
if unit == 'rad':
angle = 360*angle/(2*3.14159)
- if angle ==90:
- return clip.fl_image(lambda im : im.T[::-1],
- apply_to='mask' if apply_to_mask else [])
- elif angle == -90:
- return clip.fl_image(lambda im : im.T,
- apply_to='mask' if apply_to_mask else [])
- elif angle == 180:
- return clip.fl_image(lambda im : im[::-1,::-1],
- apply_to='mask' if apply_to_mask else [])
+
+ transpo = [1,0] if clip.ismask else [1,0,2]
+ angle_transfo = {
+ 90 : lambda im : np.transpose(im, axes=transpo)[::-1],
+ -90: lambda im : np.transpose(im, axes=transpo)[:,::-1],
+ 180: lambda im : im[::-1,::-1]}
+
+ if angle in angle_transfo:
+ newclip = clip.fl_image( angle_transfo[angle])
else:
- raise 'Angle not supported, only 90, -90, 180'
+ raise ValueError('Angle not supported, only 90, -90, 180')
+
+ if apply_to_mask and (newclip.mask is not None):
+ newclip.mask = newclip.mask.fx( rotation, angle,
+ apply_to_mask=False,
+ unit=unit)
+
+ return newclip