Making animations using moviepy


In [1]:
%matplotlib inline

In [2]:
import matplotlib.pyplot as plt
import numpy as np
from moviepy.video.io.bindings import mplfig_to_npimage
import moviepy.editor as mpy

# DRAW A FIGURE WITH MATPLOTLIB

duration = 2

fig_mpl, ax = plt.subplots(1,figsize=(5,3), facecolor='white')
xx = np.linspace(-2,2,200) # the x vector
zz = lambda d: np.sinc(xx**2)+np.sin(xx+d) # the (changing) z vector
ax.set_title("Elevation in 0")
ax.set_ylim(-1.5,2.5)
line, = ax.plot(xx, zz(0), lw=3)

# ANIMATE WITH MOVIEPY (UPDATE THE CURVE FOR EACH t). MAKE A GIF.

def make_frame_mpl(t):
    line.set_ydata( zz(2*np.pi*t/duration))  # <= Update the curve
    return mplfig_to_npimage(fig_mpl) # RGB image of the figure

animation =mpy.VideoClip(make_frame_mpl, duration=duration)
animation.write_videofile("sinc_mpl.mp4", fps=20)


Imageio: 'libfreeimage-3.16.0-linux64.so' was not found on your computer; downloading it now.
Try 1. Download from https://github.com/imageio/imageio-binaries/raw/master/freeimage/libfreeimage-3.16.0-linux64.so (4.6 MB)
Downloading: 8192/4830080 bytes (0.2%)442368/4830080 bytes (9.2%)1777664/4830080 bytes (36.8%)4830080/4830080 bytes (100.0%)
  Done
File saved as /nfshome/bjshaw/.imageio/freeimage/libfreeimage-3.16.0-linux64.so.
Imageio: 'ffmpeg.linux64' was not found on your computer; downloading it now.
Try 1. Download from https://github.com/imageio/imageio-binaries/raw/master/ffmpeg/ffmpeg.linux64 (27.2 MB)
Downloading: 8192/28549024 bytes (0.0%)442368/28549024 bytes (1.5%)1794048/28549024 bytes (6.3%)4587520/28549024 bytes (16.1%)8568832/28549024 bytes (30.0%)12648448/28549024 bytes (44.3%)15769600/28549024 bytes (55.2%)18972672/28549024 bytes (66.5%)22855680/28549024 bytes (80.1%)27074560/28549024 bytes (94.8%)28549024/28549024 bytes (100.0%)
  Done
WARNING:py.warnings:/usr/local/lib/python3.4/dist-packages/skimage/filter/__init__.py:6: skimage_deprecation: The `skimage.filter` module has been renamed to `skimage.filters`.  This placeholder module will be removed in v0.13.
  warn(skimage_deprecation('The `skimage.filter` module has been renamed '

File saved as /nfshome/bjshaw/.imageio/ffmpeg/ffmpeg.linux64.
[MoviePy] >>>> Building video sinc_mpl.mp4
[MoviePy] Writing video sinc_mpl.mp4
[MoviePy] Done.
[MoviePy] >>>> Video ready: sinc_mpl.mp4 


In [ ]: