ImageSequence Module
ImageSequence is a module in the Python Pillow library (a fork of PIL), specialized for handling multi-frame images (such as GIF, TIFF, and other formats).
The ImageSequence module lets you access each frame of an image sequence iteratively, making it ideal for animated images or image files containing multiple pages.
Main uses of ImageSequence
- Processing animated GIFs: read and manipulate a GIF animation frame by frame
- Processing multi-page TIFFs: access the individual pages in a TIFF file
- Image sequence processing: batch process a single file containing multiple images
- Frame extraction and analysis: extract specific frames from an animated image for analysis or modification
Core methods of the ImageSequence module
Here are the most commonly used methods of the ImageSequence module and what they do:
| Method/attribute | Description | Return Value | Example |
|---|---|---|---|
| ImageSequence.Iterator(image) | Create an iterator object for traversing the image sequence | Iterator object | for frame in ImageSequence.Iterator(img): |
| ImageSequence.all_frames(image) | Return a list containing all frames | List | frames = list(ImageSequence.all_frames(img)) |
| seek(frame) | Jump to a specified frame (supported by some image formats) | None | img.seek(5) # jump to the 6th frame |
| tell() | Return the index position of the current frame | Integers | current_frame = img.tell() |
| n_frames | Return the total number of frames in the image | Integers | total_frames = img.n_frames |
Usage examples
1. Basic usage: iterate over all frames of a GIF animation
Example
from
PIL
import
Image
,
ImageSequence
Open the GIF file
with
Image.
open
(
'animation.gif'
)
as
img:
Iterate over each frame
for
i
,
frame
in
enumerate
(
ImageSequence.
Iterator
(
img
)
)
:
Process each frame
frame.
save
(
f
'frame_{i}.png'
)
Save as a separate PNG file
2. Extract all pages from a TIFF file
Example
from
PIL
import
Image
,
ImageSequence
Open the multi-page TIFF file
with
Image.
open
(
'multipage.tiff'
)
as
img:
Get all frames
frames
=
list
(
ImageSequence.
all_frames
(
img
)
)
Process each page
for
i
,
page
in
enumerate
(
frames
)
:
page.
save
(
f
'page_{i}.jpg'
,
quality
=
95
)
3. Modify and save an animated GIF
Example
from
PIL
import
Image
,
ImageSequence
Open the source GIF
with
Image.
open
(
'source.gif'
)
as
img:
Prepare the new frame list
new_frames
=
[
]
Process each frame
for
frame
in
ImageSequence.
Iterator
(
img
)
:
Modify each frame (e.g., add text)
modified_frame
=
frame.
copy
(
)
... add your modification code here ...
new_frames.
append
(
modified_frame
)
Save the modified GIF
new_frames
[
0
]
.
save
(
'modified.gif'
,
save_all
=
True
,
append_images
=
new_frames
[
1
:
]
,
duration
=
img.
info
.
get
(
'duration'
,
100
)
,
loop
=
0
)
Notes
- Memory Usage: When processing large multi-frame images, pay attention to memory consumption; consider processing frame by frame rather than loading all frames at once
- Format support: Not all image formats support multi-frame operations; common supported formats include GIF, TIFF, WebP, etc.
- Frame index: Frame indices start at 0, which may differ from what common image viewers display (viewers usually start counting from 1)
- Modifying the original image: Directly modifying frames may affect the original image; it is recommended to use
frame.copy()create a copy before modifying
Summary
The ImageSequence module provides simple yet powerful tools for working with multi-frame images. Through its iterator interface, developers can easily access and manipulate each frame in an animated or paginated image. Whether extracting frames, analyzing content, or creating new animations, this module greatly simplifies the workflow.