In the world of “memes”, you might want GIF(Graphics Interchange Format) to make your conversation funny with your friends. GIF is the middle format between image and video. It is animated but not a complete video. It is an exceptional choice when you don’t want to insert video but want to add dynamic images.
The easiest way to create an animated GIF in Python is to import the “imageio” library and use its “.imwrite()” function. Ensure that all the images you pass as input have the same height and width.
Dimensions must be the same for all the images that will soon be converted to animated gifs.
For this tutorial, I will be using the below four images and creating a GIF from them:
Decision Tree Diagram
The above decision tree diagram depicts the step-by-step process of creating a GIF from multiple images. We used the imagio library to open each image from the library of the images and append each image to the list.
Finally, use the imwrite() function to save the list of images as GIFs.
Install the imageio library using the command below if not installed:
pip install imageio
Code example
import imageio.v3 as iio # List of images that will be converted to gif list_of_images = ['k1.jpg', 'k2.jpg', 'k3.jpg', 'k4.jpg'] # Empty list that will store the image data read from the files. images_arr = [] # Loop through each image and read each image file into an array for filename in list_of_images: images_arr.append(iio.imread(filename)) # Save the sequence of images as an animated gif iio.imwrite('krunal.gif', images_arr, duration=500, loop=0) print("Animated GIF (krunal.gif) created successfully")
Output
Animated GIF (krunal.gif) created successfully
The .imread() method reads each image file into an array and returns its pixel data as a numpy array.
The .imwrite() method saves the sequence of images as an animated GIF.
The duration argument of the .imwrite() function specifies the duration (in milliseconds) for each frame in the GIF. Each image will be displayed for 500 milliseconds before the next image.
The imageio library is fast but if the number of images is plenty then processing each image can make the overall process slow. It is ideal for generating animated charts or plots.
GIF only supports a limited color palette (256 colors), which can affect image quality.
Working with different-sized images
The above approach assumes that we are working with same-sized multiple images. What if all the list of images have different dimensions? Well, in that case, it will throw an error like this: ValueError: all input arrays must have the same shape. To fix this error, we have to resize all the images in the same shape and then create a gif based on those resized images.
To resize an image, we can use the “PIL” library and also the “numpy” library. We can install the both libraries using the command below:
pip install imageio pip install numpy
Here is the Python code that deals with resizing and making GIFs:
import imageio.v3 as iio from PIL import Image import numpy as np # Creating a list of images that will be saved as gif filenames = ['k1.jpg', 'k2.jpg', 'k3.jpg', 'k4.jpg'] # An empty image array that will be filled with image data array main_images = [] # Defining the desired output size (Width x Height) output_size = (1000, 1000) for filename in filenames: img = Image.open(filename).convert('RGB') # Ensuring RGB color img_resized = img.resize(output_size, Image.LANCZOS) # Resizing the image # Converting the resized image to numpy array img_array = np.asarray(img_resized) main_images.append(img_array) # Saving the images as an animated GIF iio.imwrite('krunal.gif', main_images, duration=500, loop=0)
And it will give us the output without returning any error because we are modifying the size of the images. If you feed the same-sized images then it will work fine but if it is not then it will give you an error.