Pillow is a powerful Python library for image processing tasks. It provides the ImageDraw module, which allows you to create and modify images. In this tutorial, we will explore how to create and modify animated images using Pillow’s ImageDraw module.
Installation
Before we start, make sure you have Pillow installed. You can install it using pip with the following command:
pip install pillow
Creating an Animated Image
To create an animated image, we need to use the ImageSequence
module of Pillow. This module allows us to create a sequence of images that will be displayed in a loop, creating the illusion of animation.
Here’s an example of how to create a simple animated image using Pillow:
from PIL import Image, ImageSequence
# Create a new image with a black background
image = Image.new("RGB", (500, 500), "black")
# Create a list to store the frames of the animation
frames = []
# Create a frame for each desired animation state
for i in range(10):
# Create a new frame
frame = image.copy()
# Draw something on the frame
draw = ImageDraw.Draw(frame)
draw.rectangle([(i * 50, i * 50), ((i + 1) * 50, (i + 1) * 50)], fill="white")
# Add the frame to the list of frames
frames.append(frame)
# Save the frames as an animated GIF
frames[0].save("animation.gif", save_all=True, append_images=frames[1:], optimize=False, duration=200, loop=0)
In this example, we create a black image with a size of 500x500 pixels. Then, we loop through a range of 10, creating a frame for each animation state. Each frame is created by copying the original image, drawing something on it using the ImageDraw
module, and adding it to the list of frames.
Finally, we save the frames as an animated GIF using the save
method of the first frame. The save_all
parameter ensures that all frames are included in the animated GIF, the append_images
parameter specifies the frames to append after the first frame, the optimize
parameter disables optimization of the GIF, the duration
parameter sets the duration of each frame in milliseconds, and the loop
parameter sets the number of times the animation should loop (0 means infinite looping).
Modifying an Animated Image
Once we have an animated image, we can modify it by applying various transformations to each frame. For example, we can rotate, resize, or apply filters to the frames to create different effects.
Here’s an example of how to modify an animated image by rotating each frame:
from PIL import Image, ImageSequence
# Open the animated image
image = Image.open("animation.gif")
# Create a list to store the modified frames
modified_frames = []
# Iterate through each frame
for frame in ImageSequence.Iterator(image):
# Rotate the frame by 45 degrees clockwise
modified_frame = frame.rotate(-45)
# Add the modified frame to the list
modified_frames.append(modified_frame)
# Save the modified frames as a new animated GIF
modified_frames[0].save("modified_animation.gif", save_all=True, append_images=modified_frames[1:], optimize=False, duration=200, loop=0)
In this example, we open an existing animated GIF using Pillow’s open
method. Then, we iterate through each frame using the Iterator
function of the ImageSequence
module. Inside the loop, we rotate each frame by 45 degrees clockwise using the rotate
method and add the modified frame to the list of modified frames.
Finally, we save the modified frames as a new animated GIF using the save
method of the first modified frame, with the same parameters as before.
Conclusion
In this tutorial, we have explored how to create and modify animated images using Pillow’s ImageDraw module. We have seen how to create an animated image by creating a sequence of frames and saving them as an animated GIF. We have also learned how to modify an existing animated image by applying transformations to each frame.
Pillow provides many other features for image processing and manipulation. If you want to learn more about Pillow, check out the official documentation.