Skip to content
Related Articles

Related Articles

Create and save animated GIF with Python – Pillow
  • Last Updated : 24 Feb, 2021

Prerequisites: pillow

In this article, we are going to use a pillow library to create and save gifs.

GIFs: The Graphics Interchange Format(gif) is a bitmap image format that was developed by a team at the online services provider CompuServe led by American computer scientist Steve Wilhite on 15 June 1987.

A GIF file normally stores a single image, but the format allows multiple images to be stored in one file. The format also has parameters that can be used to sequence the images to display each image for a short time then replace it with the next one. In simple words, GIF is a moving image.

Pillow: Pillow is used for image processing in python. Pillow was developed on top of PIL(python image library). PIL was not supported in python 3, so we use a pillow. 



This module is not preloaded with Python. So to install it execute the following command in the command-line:

pip install pillow

Let’s create a gif in step wise:

Step 1: First we import our requirements for PIL module.

Python3




from PIL import Image, ImageDraw

Step 2: Create a list after we enter the values of the circle. (0,255,0) it is the color code of green and (255,0,0) is the color code of red.

Python3




images = []
width = 200
center = width // 2
color_1 = (0,255, 0)
color_2 = (255, 0, 0)
max_radius = int(center * 1.5)
step = 8

Step 3: For loop was used to create an animation image.2nd line of code used to set values of the square, that square contains red color and it’s edge size is 200.3rd line used to create square image .4th line used to draw a circle in that square image, that circle color is green.

Python3






for i in range(0, max_radius, step):
    im = Image.new('RGB', (width, width), color_2)
    draw = ImageDraw.Draw(im)
    draw.ellipse((center - i, center - i,
                  center + i, center + i), 
                 fill=color_1)
    images.append(im)

Step 4: Save the gif image.

Python3




images[0].save('pillow_imagedraw.gif',
               save_all = True, append_images = images[1:],
               optimize = False, duration = 10)

Below is the full implementation:

Python3




from PIL import Image, ImageDraw
  
images = []
  
width = 200
center = width // 2
color_1 = (0,255, 0)
color_2 = (255, 0, 0)
max_radius = int(center * 1.5)
step = 8
  
for i in range(0, max_radius, step):
    im = Image.new('RGB', (width, width), color_2)
    draw = ImageDraw.Draw(im)
    draw.ellipse((center - i, center - i,
                  center + i, center + i),
                 fill = color_1)
    images.append(im)
  
images[0].save('pillow_imagedraw.gif',
               save_all = True, append_images = images[1:], 
               optimize = False, duration = 10)

  Output:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :