Extract Each Frame of an Animated GIF Using Python Pillow

By | September 22, 2020

Each animated gif image contains some frames. In this tutorial, we will introduce how to extract these frames using python pillow library.

Preliminary

We should import some libraries

from PIL import Image, ImageSequence

Open an animated gif file

image_old = 'gif-test.gif'
im = Image.open(image_old)

We can get some useful information on this gif image.

print(im.mode)
print("gif frames = " + str(im.n_frames))

Run this code, we can find gif-test.gif image file contains 15 frames.

P
gif frames = 15

Extract each frame from gif image

We can use ImageSequence to iterate each frame and save these frames to image files.

for frame in ImageSequence.Iterator(im):
    i += 1
    frame.save("gif-webp-"+str(i)+".webp",format = "WebP", lossless = True)

In this tutorial, we will save each frame of gif image to webp image.

Extract Each Frame of an Animated GIF Using Python Pillow

Leave a Reply