Programmatically generate video or animated GIF in Python? Programmatically generate video or animated GIF in Python? python python

Programmatically generate video or animated GIF in Python?

I'd recommend not using images2gif from visvis because it has problems with PIL/Pillow and is not actively maintained (I should know, because I am the author).

Instead, please use imageio, which was developed to solve this problem and more, and is intended to stay.

Quick and dirty solution:

import imageioimages = []for filename in filenames:    images.append(imageio.imread(filename))imageio.mimsave('/path/to/movie.gif', images)

For longer movies, use the streaming approach:

import imageiowith imageio.get_writer('/path/to/movie.gif', mode='I') as writer:    for filename in filenames:        image = imageio.imread(filename)        writer.append_data(image)

Here's how you do it using only PIL (install with: pip install Pillow):

import globfrom PIL import Image# filepathsfp_in = "/path/to/image_*.png"fp_out = "/path/to/image.gif"#, *imgs = [ for f in sorted(glob.glob(fp_in))], format='GIF', append_images=imgs,         save_all=True, duration=200, loop=0)

See docs:

Well, now I'm using ImageMagick. I save my frames as PNG files and then invoke ImageMagick's convert.exe from Python to create an animated GIF. The nice thing about this approach is I can specify a frame duration for each frame individually. Unfortunately this depends on ImageMagick being installed on the machine. They have a Python wrapper but it looks pretty crappy and unsupported. Still open to other suggestions.