Open In App

Find the Duration of Gif Image in Python

Last Updated : 20 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Python can perform various image processing tasks, including analyzing GIF images to determine their total duration. By utilizing libraries such as PIL (Pillow) and Imageio, we can easily read GIF files and calculate their durations by summing the frame durations. In this article, we will learn to find the duration of GIF Image in Python.

The duration of a GIF can be found using various Python libraries, such as Pillow and Imageio library. Let us first learn about the concepts that are related to the topic.

Steps to Find the Duration of GIF Image

We can find the duration of a GIF image in Python by following the following steps:

  • Import Modules
  • Load GIF Image
  • Extract Frame Duration
  • Calculate Total Duration

Each steps are almost similar with a little changes based on the module that you are using.

Finding the Duration of GIF Image

Here we will see different examples to find the duration of the GIF Image in detail. We will use the following gif as an example for each method.

circle
input.gif

Using the PIL (Pillow) Library

In this approach, we are using the PIL (Pillow) library to open a GIF file and iterate through its frames. We initialize a variable to store the total duration, then loop through each frame, adding the frame's duration (in milliseconds) to the total.

We use image.seek() function to seeks through the given frame in the sequence file. The image.tell() function returns the current frame number. The loop continues until an EOFError is encountered, indicating the end of the GIF. Finally, the total duration is printed.

Python
# import pillow module
from PIL import Image

# open the GIF file
img = Image.open("input.gif")

# initialize the duration
duration = 0

# loop through the frames and add the durations
while True:
    try:
        frame_duration = img.info['duration']
        duration += frame_duration
        img.seek(img.tell() + 1)
    except EOFError:
        break
        
# printing the total duration in milliseconds
print(f"Total duration: {duration} ms")

Output:

Total duration: 4800 ms

Using the imageio Library

In this approach, we are using the imageio library to read a GIF file using the imageio.get_reader() function and iterate through its frames. The 'gif.get_meta_data()' function retrieves metadata for the current frame, including its duration. Then the duration of each frame by accessing the frame's metadata and then print the total duration in milliseconds.

Python
# import the module
import imageio

# read the GIF file
gif = imageio.get_reader("input.gif")

# Initialize the duration
duration = 0

# Loop through the frames and sum the durations
for frame in gif:
    duration += gif.get_meta_data(index=frame)["duration"]

    # Print the total duration in milliseconds
print(f"Total duration: {duration} ms")

Output:

Total duration: 4800 ms

Conclusion

Finding the duration of an animated GIF in Python is a straightforward process with the help of the Pillow and imageio libraries. By understanding the structure of GIF files and leveraging Python’s image processing capabilities, you can easily calculate the duration of each frame and the total duration of the GIF. This knowledge can be applied to a variety of applications, from simple scripts to complex image processing systems.


Next Article
Article Tags :
Practice Tags :

Similar Reads