Skip to content

Instantly share code, notes, and snippets.

@Nua07
Last active October 13, 2022 21:53
Show Gist options
  • Save Nua07/5ebfcc6482723180d3971dd079e994d4 to your computer and use it in GitHub Desktop.
Save Nua07/5ebfcc6482723180d3971dd079e994d4 to your computer and use it in GitHub Desktop.
from PIL import Image, ImageSequence
def webp2gif(data):
img = Image.open(io.BytesIO(data))
frames = []
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').convert(
'P', palette=Image.ADAPTIVE, colors=255))
out = io.BytesIO()
frames[0].save(out, 'gif', save_all=True, append_images=frames[1:],
duration=img.info.get("duration", 10), loop=0, quality=100)
out.seek(0)
return out.getvalue()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment