Skip to content

Creating GIFs in Python using Pillow (PIL Fork)

I was working on a personal project the other day and I needed to create some images (frames) and save them as a playable GIF. Working in Python, I excepted to find an easy solution fast but oh boy did it take me too long to find it.
Here I am now, creating a blog post to help future people looking to create gifs in Python.

The code used in this post is available here.

What are GIFs?

The Graphics Interchange Format is a bitmap image format that was developed in 1987.
A GIF is basically a series of images that have different settings such as:

  • Looping
  • Duration per frame (image)
  • Others…

A GIF can also be a static image.

An example of a GIF (Nyan Cat ♥)
An example of a GIF (Nyan Cat ♥)

What is Pillow (PIL Fork)?

Python Imaging Library (PIL) is a free library for the Python programming language that adds support for opening, manipulating, and saving many different image file formats.
On the other hand, Pillow is a fork of the PIL library introducing many changes and enhancements, whether to make the API easier to use or to handle more image formats and requirements.

N.B: PIL and Pillow cannot co-exist, you can only install/use one or the other.

Creating GIFs in Python

Installing Pillow

You can easily install Pillow using Pip:

Creating GIFs

As an example, I will be creating a GIF where a ball moves down.

At first, we’ll create a method that creates an image and draws a ball on it, this will help us keep things clean.

In the code above, we create a new RGB image using that has a certain size and a white background.
We then use ImageDraw to draw a red ellipse (circle) into our image using the coordinates given in the arguments.

All we have to do now is create multiple images (frames) while moving the ball down:

Here’s the breakdown of the code above:

  1. Initialize an empty list frames and 0-coordinates x and y.
  2. In a for loop (10 runs), we create a 400×400 image with a ball in our coordiantes.
  3. Change the coordinates so that the ball moves down (in diagonal, since the Y position changes too).
  4. Save the first frame with the others using format='GIF' and append_images=frames[1:].
    • Every frame will run for 100ms (duration=100).
    • The GIF image will loop forever (loop=0, if loop was set to 1 then it will loop 1 time, 2 = 2 times, etc…).

Our magnificent moving ball looks like this:

Moving ball
Moving ball


I hope this can help anyone looking for a way to handle images and create GIFs!

Published inProgramming

Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Python Development Recent comment authors
newest oldest most voted
Notify of
Python Development
Python Development

Nice blog…Thanks sharing..