Hack: How to create GIF Images in Python

[This article was first published on Python – Predictive Hacks, and kindly contributed to python-bloggers]. (You can report issue about the content on this page here)
Want to share your content on python-bloggers? click here.

We will show how you can create GIF images in Python from a set of images. We will work with the imageio library and where we can install it as follows:

  • If you are in a conda env: conda install -c conda-forge imageio
  • If you have pip: pip install imageio

For this example, I used three images of Zach LaVine from a Slam Dunk contest.

The three images:

Hack: How to create GIF Images in Python 1
Hack: How to create GIF Images in Python 2
Hack: How to create GIF Images in Python 3

Let’s start:

import imageio
import os
files = os.listdir('my_images')

# assume that your images that you 
# want to make the GIF are under the my_images folder
images_path = [os.path.join('my_images',file) for file in files]

# fps are the frames per second
images = []
for img in images_path:
    images.append(imageio.imread(img))
imageio.mimsave('output/mygif.gif', images, fps=2)
 

voilà the GIF:

Hack: How to create GIF Images in Python 4
To leave a comment for the author, please follow the link and comment on their blog: Python – Predictive Hacks.

Want to share your content on python-bloggers? click here.