Rotating a color clip in MoviePy?

Viewed 951

I'm trying to rotate a moviepy color clip without animating it, so that it is rotated at the start, and remains rotated until the end of the video.

I tried some code hoping that it would rotate a still image in MoviePy, without animating it. So that it is rotated by the input angle for the entire duration of the image, ColorClip, or Video.

import moviepy.editor as mped
image = mped.ImageClip("image.jpg", duration=3)
image.rotate(20)
image.write_videofile('image_without_rotation.mp4', fps=5)

However, this doesn't rotate the still image, because ImageClip doesn't have a rotation function.

This code below rotates until 3 seconds. and can be adapted for a fixed rotation / non animated by changing 20*t to the a constant like 20.

clip = mped.ImageClip('image.jpg')
rotated_clip = (clip.add_mask().fx(mped.vfx.rotate, lambda t: 20*t, expand=False).set_duration(3))
centered_rotated_clip = mped.CompositeVideoClip([rotated_clip.set_pos("center")])
centered_rotated_clip.write_videofile('rotated_image.mp4'), fps=10)

However, this code won't work for a moviepy ColorClip.

color_clip = mped.ColorClip(size=[300, 300], color=[1 ,1, 1])
rotated_color_clip = (color_clip.fx(mped.vfx.rotate, lambda t: 20, expand=False).set_duration(3))
composited_clip = mped.CompositeVideoClip([rotated_Color_ clip.set_pos("center")])
composited_clip.write_videofile('rotated_clip.mp4', fps=10)

instead it will give the following error

    raise TypeError("Cannot handle this data type: %s, %s" % typekey)
TypeError: Cannot handle this data type: (1, 1, 3), <i8

because it reaches this line

return np.array( Image.fromarray(pic).rotate(angle, expand=expand),

inside pil_rotater.

How can I rotate a moviepy ColorClip by 20 degrees ?

1 Answers

Update 2: This fix is included in v2.0.0.dev1. Install from pip with pip install --pre --upgrade moviepy.

Update: I’ve found the bug, and you can track the progress of the bug fix here: https://github.com/Zulko/moviepy/pull/1139

In the mean time, as a workaround, instead of passing color=[150, 100, 200] to ColorClip, pass color=np.array([150, 100, 200]).astype(np.uint8).

———

The code

image = mped.ImageClip("image.jpg", duration=3)
image.rotate(20)
image.write_videofile('image_without_rotation.mp4', fps=5)

doesn't work because image.rotate() returns a new clip, so you must do image = image.rotate(20).

This should work for both ImageClips and ColorClips, however it appears that you have found a bug where rotations do not work for ColorClips. The best thing to do would be to report it as an issue at github.com/zulko/moviepy, or try and find a fix yourself and submit it as a pull request there.

For the time being, create a ColorClip, write it to a file, and then read it in again as an ImageClip.

Related