The faster way is to use imageio
as in @ShlomiF's answer, but you can do the same thing with pure matplotlib if you so prefer:
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
nframes = 25
plt.subplots_adjust(top=1, bottom=0, left=0, right=1)
def animate(i):
im = plt.imread('image'+str(i)+'.png')
plt.imshow(im)
anim = FuncAnimation(plt.gcf(), animate, frames=nframes,
interval=(2000.0/nframes))
anim.save('output.gif', writer='imagemagick')
But if your first priority is the quality of the output you may want to consider using ffmpeg
and convert
directly,
ffmpeg -f image2 -i image%d.png output.mp4
ffmpeg -i output.mp4 -vf "fps=10,scale=320:-1:flags=lanczos" -c:v pam -f \
image2pipe - | convert -delay 10 - -loop 0 -layers optimize output.gif
Changing the scale
argument as needed to control the size of the final output, scale=-1:-1
leaves the size unchanged.