Predictive Hacks

Hack: How to create GIF Images in Python

gif

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

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

Python

Get Started with Hugging Face Auto Train

Hugging Face has launched the auto train, which is a new way to automatically train, evaluate and deploy state-of-the-art Machine