Skip to content

Instantly share code, notes, and snippets.

@Zulko
Last active December 15, 2020 15:03
Show Gist options
  • Save Zulko/9e65c119b9c9ad027608 to your computer and use it in GitHub Desktop.
Save Zulko/9e65c119b9c9ad027608 to your computer and use it in GitHub Desktop.
Gif with a cat jumping in a box
# Result: http://i.imgur.com/Lmj21mR.gif
import moviepy.editor as mpy
from moviepy.video.tools.drawing import color_split
# LOAD THE GIF SEVERAL TIMES
clip = VideoFileClip("./source.gif")
W,H = SIZE = clip.size
duration = clip.duration
left = VideoFileClip("./source.gif").crop(x2=W/2)
right = VideoFileClip("./source.gif").crop(x1=W/2)
# WORK ON THE LEFT PART (JUMPING CAT)
T=.65 # period for the jumping cat
GW= 30 # width of the mask's gradient black/white
def mask_mf(t):
x= max(-GW,min(W/3,(t-T)*W/duration/1.3-30))
return color_split(SIZE,x=x, grad_width=GW)
mask = VideoClip(mask_mf, ismask=True, duration=duration)
left_mask = left.set_mask(mask)
compo_left = (CompositeVideoClip(
[left_mask.set_start(i*T)
for i in [3,2,1,0]])
.subclip(T,3*T))
# WORK ON THE RIGHT PART (MAKE IT LOOPABLE)
compo_right = (right.subclip(0.6,0.9)
.fx(vfx.time_symmetrize)
.speedx(final_duration=2*T))
# ASSEMBLE LEFT AND RIGHT, WRITE TO A FILE
final = clips_array([[compo_left, compo_right]])
final.write_gif("cat_in_a_box.gif", fps=15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment