Skip to content

Instantly share code, notes, and snippets.

@Seneketh
Forked from nimatrueway/README.md
Created March 1, 2022 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Seneketh/f570597b07b6ca9acefa5c6345f43b39 to your computer and use it in GitHub Desktop.
Save Seneketh/f570597b07b6ca9acefa5c6345f43b39 to your computer and use it in GitHub Desktop.
Convert webp animations to gif preserving image quality (RGB = 24bit)
#!/usr/bin/env python3
from sys import argv
from pathlib import Path
from PIL import Image, ImageSequence
def webp2gif(path: Path):
img = Image.open(path)
frames: list[Image.Image] = []
for frame in ImageSequence.Iterator(img):
im2 = Image.new("RGB", frame.size, (255, 255, 255))
im2.paste(frame, mask=frame.split()[3])
frames.append(im2.convert('RGB'))
frames[0].save(path.with_suffix('.gif'),
format='gif',
save_all=True,
append_images=frames[1:],
optimize=True,
duration=img.info.get("duration", 10),
loop=img.info.get("loop", 0),
quality=100)
webp2gif(Path(argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment