Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Creating an Animated GIF with Python

  • 23 Oct, 2024
  • Com 0
Creating an Animated GIF with Python

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:

k2k3k4

Decision Tree Diagram

Decision Tree Diagram of Creating an Animated GIF with PythonThe 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

krunal gif

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.

Post Views: 51
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Converting a PIL Image to OpenCV with Python
Adding Text to Images using Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend