Contact Form

Name

Email *

Message *

Cari Blog Ini

Python Gif Animation

```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:

  1. Install Pillow (if not already installed) using pip install Pillow
  2. Import the required modules from Pillow: from PIL import Image, ImageSequence
  3. Load the sequence of images into a list using Image.open()
  4. Create an empty GIF file using Image.new()
  5. Iterate over the sequence of images and append them to the GIF file using ImageSequence.append()
  6. Save the GIF file using Image.save()
  7. 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