”;
Introduction to Image Sequences
Image sequences in Pillow (Python Imaging Library) refer to a collection of individual images displayed in a specific order to create an animation. The popular file formats of the image sequences are GIFs, APNG (Animated Portable Network Graphics), FLI/FLC, TIFF files(with more than one frame), and more.
The Python Pillow library provides the ImageSequence module to work with image sequences and animated pictures. The main feature of this ImageSequence module is, its ability to iterate over the frames of an image sequence. We can do this by using the ImageSequence.Iterator class which acts as a wrapper for image sequences and simplifies the process of accessing individual frames.
Reading Image Sequences
Reading image sequences is a fundamental operation when working with animations or multi-frame images. You can open and read an image sequences using the Image.open() method in the Python Pillow (PIL) library.
Example
Here”s an example demonstrating how to read image sequences using the Python Pillow Image.open() method.
from PIL import Image, ImageSequence # Open the image sequence file image = Image.open("Images/Book_animation.gif") # Display the opened image image.show()
Output
While executing the above code we get the following output −
When an image sequence file is opened, PIL automatically loads the first frame. To access other frames in the sequence, we need to iterate through them using the ImageSequence.Iterator class, which provides a convenient way to loop through all the frames in the image sequence.
Accessing Frames in an Image Sequence
The ImageSequence.Iterator() class in Python Pillow allows us iterate over the frames of an image sequence. It accepts an image object as a parameter and implements an iterator object that can be used by a user to iterate over an image sequence. We can use the operator to access individual frames in the sequence or we can use the for loop to iterate over all of the frames.
The syntax for using the ImageSequence.Iterator() class is as follows −
image_object = PIL.ImageSequence.Iterator(image)
where image_object is the iterator used to loop over the frames of the image sequence, and image is our input image object (an instance of PIL.Image.)
To work with ImageSequence.Iterator in Pillow, follow these steps
-
Import necessary modules such as Image and ImageSequence.
-
Load the first image in the sequence using the Image.open() method.
-
Iterate through the frames using PIL.ImageSequence.Iterator() class.
-
Within the loop, process or manipulate each frame as needed, which can include any of the following operations such as resizing, adding text, applying filters, or other image manipulation techniques available in Pillow.
-
Save the processed frames as separate images, using the save() method.
Example
Below is an example that demonstrates using ImageSequence.Iterator() class to iterate over the frames of an image sequence.
from PIL import Image, ImageSequence # Open an image sequence file image = Image.open("Images/Book_animation.gif") index = 0 # Iterate through the frames for frame in ImageSequence.Iterator(image): # Process or manipulate the current frame here frame.convert(''RGB'').save("output_image/frame % d.jpg" % index) index += 1 print("All frames of the ImageSequence are saved separately.")
Output
All frames of the ImageSequence are saved separately.
When you execute the above code, all frames of the image sequence are saved separately in your working directory.
We can also access various properties of individual frames within an image sequence. For example we can retrieve the duration of each frame which determines how long it is displayed in an animation.
Creating Image Sequences
While the primary use of the Image Sequence module is to extract frames from existing sequences we can also create our own image sequences. Learn more about Creating Animated GIFs here.
Example
Here is a basic example that demonstrating how to create a GIF image sequence from a list of image.
from PIL import Image # Create a list of image objects images = [Image.open(f"Images/split{i}.jpg") for i in range(1,4)] # Save the image sequence as a GIF images[0].save( "output_image/creating_animation.gif", save_all=True, append_images=images[1:], duration=200, loop=0 ) print("Image sequence created and saved as creating_animation.gif")
Output
Image sequence created and saved as creating_animation.gif
Modifying and Saving Image Sequences
Image sequences can be manipulated and saved just like regular images. For modifying the image sequences you can use the all_frames() method from the Pillow”s ImageSequence module. This method applies a specified function to all frames in the image sequence and returns a list containg the all separated frames. By using this method, we can iterate over each frame in an image sequence and apply the necessary transformations.
Following is the syntax of the ImageSequence.all_frames() method −
PIL.ImageSequence.all_frames(im,func)
Where,
-
im − An image sequence.
-
func − A function to apply to all of the image frames.
After making changes to an image sequence we can save it as a new image sequence or a different format depending on our needs.
Example
This example demonstrates how to add text to each frame of an image sequence and save the modified sequence.
from PIL import Image, ImageSequence, ImageDraw # Open the image sequence im = Image.open(''Images/book_animation.gif'') # Define a function to add text to each frame def add_text(im_frame): draw = ImageDraw.Draw(im_frame) draw.text((150, 100), "Tutorialspoint", fill=''green'') return im_frame # Apply the add_text function to all frames in the image sequence ims = ImageSequence.all_frames(im, add_text) # Save the modified frames as a new GIF ims[0].convert(''RGB'').save(''output_image/modified_image_sequences.gif'', save_all=True, append_images=ims[1:]) print(''Text added to each frame of the given ImageSequence.'')
Output
Text added to each frame of the given ImageSequence.
Conclusion
Pillow”s Image Sequence module is a powerful tool for working with image sequences and creating animations. We can easily iterate over frames, extract properties and apply various image operations to create dynamic visuals. Whether we are working with GIFs or creating custom animations or processing image sequences Pillow provides the necessary functions and classes to simplify the process.
”;