How to resize an animated GIF using Pillow?

Nov 06, 2025

Leave a message

Hey there! If you're into working with animated GIFs and you're using Python, you've probably come across the Pillow library. I'm a supplier of Pillow products, and I'm here to walk you through the process of resizing an animated GIF using Pillow. It's a pretty handy skill, whether you're a developer, a designer, or just someone who loves playing around with images.

Why Resize an Animated GIF?

Before we dive into the how-to, let's talk about why you might want to resize an animated GIF in the first place. There are a few reasons. Maybe you're working on a website, and you need to optimize the GIF's size to make your page load faster. Or perhaps you're creating a social media post, and you need to fit the GIF within a specific dimension. Whatever the reason, resizing an animated GIF can be super useful.

Prerequisites

First things first, you'll need to have Pillow installed. If you haven't already, you can install it using pip. Just open up your terminal and run the following command:

pip install pillow

Once you've got Pillow installed, you're ready to start resizing those GIFs.

The Process

Let's break down the process of resizing an animated GIF using Pillow into a few simple steps.

Step 1: Import the Necessary Libraries

The first thing you need to do is import the Image module from the Pillow library. Here's how you can do it:

from PIL import Image

Step 2: Open the Animated GIF

Next, you need to open the animated GIF file. You can do this using the Image.open() method. Here's an example:

gif = Image.open('your_animated_gif.gif')

Replace 'your_animated_gif.gif' with the actual path to your GIF file.

Step 3: Define the New Size

Now, you need to decide on the new size for your GIF. You can do this by specifying the width and height in pixels. For example, if you want to resize your GIF to 300 pixels wide and 200 pixels high, you can define the new size like this:

new_size = (300, 200)

Step 4: Resize Each Frame

An animated GIF is made up of multiple frames. To resize the entire GIF, you need to resize each frame individually. You can do this using a loop. Here's an example:

Household PillowMemory Foam Pillow

frames = []
for frame in range(0, gif.n_frames):
    gif.seek(frame)
    resized_frame = gif.resize(new_size, Image.LANCZOS)
    frames.append(resized_frame)

In this code, we're using a for loop to iterate through each frame of the GIF. We're then using the seek() method to move to the current frame, and the resize() method to resize the frame to the new size. Finally, we're appending the resized frame to the frames list.

Step 5: Save the Resized GIF

Once you've resized all the frames, you need to save the resized GIF. You can do this using the save() method. Here's an example:

frames[0].save('resized_animated_gif.gif', save_all=True, append_images=frames[1:], duration=gif.info['duration'], loop=0)

In this code, we're saving the first frame of the frames list as the new GIF. We're using the save_all=True parameter to indicate that we want to save all the frames, and the append_images=frames[1:] parameter to append the remaining frames to the first frame. We're also using the duration=gif.info['duration'] parameter to set the duration of each frame to the same as the original GIF, and the loop=0 parameter to indicate that we want the GIF to loop indefinitely.

Putting It All Together

Here's the complete code for resizing an animated GIF using Pillow:

from PIL import Image

# Open the animated GIF
gif = Image.open('your_animated_gif.gif')

# Define the new size
new_size = (300, 200)

# Resize each frame
frames = []
for frame in range(0, gif.n_frames):
    gif.seek(frame)
    resized_frame = gif.resize(new_size, Image.LANCZOS)
    frames.append(resized_frame)

# Save the resized GIF
frames[0].save('resized_animated_gif.gif', save_all=True, append_images=frames[1:], duration=gif.info['duration'], loop=0)

Tips and Tricks

  • Choose the Right Resampling Filter: When resizing an image, you can choose different resampling filters. The Image.LANCZOS filter is generally a good choice for high-quality resizing, but you can experiment with other filters like Image.BILINEAR or Image.NEAREST to see which one works best for your GIF.
  • Optimize the GIF Size: If you're resizing the GIF to reduce its file size, you can try using the optimize=True parameter when saving the GIF. This will remove any unnecessary metadata from the GIF and can significantly reduce its file size.

Related Pillow Products

As a Pillow supplier, I offer a wide range of products to meet your needs. Whether you're looking for a Memory Foam Pillow for a comfortable night's sleep or a Household Pillow for everyday use, I've got you covered.

Contact Us for Procurement

If you're interested in purchasing our Pillow products, we'd love to hear from you. Whether you're a retailer looking to stock our products or an individual looking for a high-quality pillow, we're here to help. Just reach out to us, and we'll start the procurement discussion.

References

  • Pillow Documentation: https://pillow.readthedocs.io/en/stable/
  • Python Imaging Library Handbook: https://www.effbot.org/imagingbook/

Send Inquiry