The size of an animated gif image is usually large. In order to reduce the size of it, we can convert it to an animated webp image. In this tutorial, we will introduce you how to convert a gif image to an animated webp image using python pillow.
Does python pillow support animatedwebp image?
from PIL import Image, ImageSequence from PIL import features print(features.check("webp_anim"))
Not every version of python pillow support animated webp image. We shoud use features.check(“webp_anim”) to check the version of your current python pillow support animated webp image or not.
If the result is True, you can use python pillow to convert.
If you only want to convert an animated gif to a webp image, not animated. You can read:
Best Practice to Python Convert GIF to WebP with Pillow – Python Pillow Tutorial
Meanwhile, if you want to convert one frame in animated gif to a webp image, you can read:
Extract Each Frame of an Animated GIF Using Python Pillow
In order to convert a gif image to an animated webp, we shoud get all frames of this gif image.
Get all frames image of gif
image_old = 'gif-test.gif' sequence = [] im = Image.open(image_old) for frame in ImageSequence.Iterator(im): sequence.append(frame.copy())
In this example, we save all frames of gif-test.gif into a list sequence.
Convert image list to animated webp image
Then we can use image.save() function to convert all frame of gif to an animated webp image.
image_new = 'gif-test-2.webp' sequence[0].save(image_new, save_all=True, append_images = sequence[1:])
Run this code, we can get this result:
GIF | 1,003KB | |
WebP | 795KB |