```html
Create and Save an Animated GIF with Python - Pillow - GeeksforGeeks
Introduction
Creating animated GIFs can be a fun and effective way to communicate information or share ideas. With Python and the Pillow library, you can easily create and save animated GIFs from a sequence of images. This article will provide a step-by-step guide on how to do this, using clear and concise instructions.
Creating an Animated GIF with Pillow
To create an animated GIF with Pillow, you will need to:
- Install Pillow (if not already installed) using pip install Pillow
- Import the required modules from Pillow: from PIL import Image, ImageSequence
- Load the sequence of images into a list using Image.open()
- Create an empty GIF file using Image.new()
- Iterate over the sequence of images and append them to the GIF file using ImageSequence.append()
- Save the GIF file using Image.save()
Here is an example code:
```python import os from PIL import Image, ImageSequence # Load the sequence of images filenames = [] for filename in os.listdir('images'): if filename.endswith('.png'): filenames.append(filename) # Create an empty GIF file gif = Image.new('RGB', (100, 100)) # Iterate over the sequence of images and append them to the GIF file for filename in filenames: image = Image.open(filename) gif.append(image) # Save the GIF file gif.save('animated.gif') ```This code will create an animated GIF file named "animated.gif" from the sequence of PNG files in the "images" directory.
Conclusion
Creating animated GIFs with Pillow is a simple and straightforward process. By following the steps outlined in this article, you can easily create your own animated GIFs to share with others.
```
Comments