Python: How to generate animated .gifs

The below code can take in a list of consecutive image files and stitch them together to create an animated .gif file.

  • It requires the imageio module.
  • The path to the consecutive image files is set in the image_path variable.
  • The full path to the name of the .gif file to be built is passed to imageio.mimsave().

Here is the complete code:

import imageio
import glob

image_path = r'C:\Users\christon\Desktop\processing_py\my_scripts\line_animate3_gif5\*.png'

files = glob.glob(image_path)
images = []
for file in files:
    # imageio.imread(file) creates a numpy matrix array
    # In this case a 200 x 200 matrix for every file, since the files are 200 x 200 pixels.
    images.append(imageio.imread(file))
    print file

imageio.mimsave(r'C:\Users\christon\Desktop\processing_py\my_scripts\line_animate3_gif5\line_animate3_4.gif', images)

Here is the animated .gif that was created with the above code:

241 .png files were stitched together to create one animated .gif file called line_animate3_4.gif.

UPDATE: Here is my latest code for generating animated gifs. This code now has two options for inputs, and some other new features:

  • Glob can be used to read in the .gif files from a specified directory or a custom array can be built to have more control over the order of the .gifs.
  • I also added a “natural sort” feature which requires import of the natsort module. This feature will read in all of the .gif file names and will not put “_10” and “_11” before “_2”.
  • There is a new “pause” variable that will place each .gif in the array multiple times in order to slow down the animation.

Here is the new code:

import imageio
import glob
from natsort import natsorted, ns

# Global variables / options
pause = 10
animated_gif_name = r'C:\Users\christon\Desktop\processing_py\my_scripts\PRINT_10\print10_animated.gif'


def create_animated_gif(files, animated_gif_name, pause=0):
    """Stitches an array of images together to create an animated gif.
    pause is an optional argument that if present will extend the number of frames
    that each image appears in, to slow down the animation."""
    if pause != 0:
        # Load the gifs up several times in the array, to slow down the animation
        frames = []
        for file in files:
            count = 0
            while count < pause:
                frames.append(file)
                count+=1

        print "Total number of frames in the animation:", len(frames)
        files = frames

    images = [imageio.imread(file) for file in files]
    imageio.mimsave(animated_gif_name, images)


def main():

    # Option 1: Load images straight from a directory
    # Use this if you are satisfied with the order that the images are already in.
    image_path = r'C:\Users\christon\Desktop\processing_py\my_scripts\PRINT_10\*.gif'
    files = glob.glob(image_path)

    # Sort the files into natural order
    files = natsorted(files, alg=ns.IGNORECASE)

    # Option 2: Prepare an array of images in a custom order
    ##files = ['C:\\Users\\christon\\Desktop\\processing_py\\my_scripts\\PRINT_10_rect1\\print10_rect3.gif',
    ##'C:\\Users\\christon\\Desktop\\processing_py\\my_scripts\\PRINT_10_rect1\\print10_rect2.gif',
    ##'C:\\Users\\christon\\Desktop\\processing_py\\my_scripts\\PRINT_10_rect1\\print10_rect1.gif',
    ##'C:\\Users\\christon\\Desktop\\processing_py\\my_scripts\\PRINT_10_rect1\\print10_rect6.gif',
    ##'C:\\Users\\christon\\Desktop\\processing_py\\my_scripts\\PRINT_10_rect1\\print10_rect7.gif',
    ##'C:\\Users\\christon\\Desktop\\processing_py\\my_scripts\\PRINT_10_rect1\\print10_rect8.gif',
    ##'C:\\Users\\christon\\Desktop\\processing_py\\my_scripts\\PRINT_10_rect1\\print10_rect9.gif',
    ##'C:\\Users\\christon\\Desktop\\processing_py\\my_scripts\\PRINT_10_rect1\\print10_rect10.gif',
    ##'C:\\Users\\christon\\Desktop\\processing_py\\my_scripts\\PRINT_10_rect1\\print10_rect11.gif',
    ##'C:\\Users\\christon\\Desktop\\processing_py\\my_scripts\\PRINT_10_rect1\\print10_rect12.gif',
    ##'C:\\Users\\christon\\Desktop\\processing_py\\my_scripts\\PRINT_10_rect1\\print10_rect5.gif',
    ##'C:\\Users\\christon\\Desktop\\processing_py\\my_scripts\\PRINT_10_rect1\\print10_rect4.gif']

    create_animated_gif(files, animated_gif_name, pause)

if __name__ == '__main__':
    main()

Leave a Reply