Skip to content

Instantly share code, notes, and snippets.

@helgibbons
Last active February 20, 2025 16:25
Show Gist options
  • Save helgibbons/524546edb62599892f8c70ac8f6f54ac to your computer and use it in GitHub Desktop.
Save helgibbons/524546edb62599892f8c70ac8f6f54ac to your computer and use it in GitHub Desktop.
Resize a .gif and split it into individual .png frames
# Resize a gif and convert it to a series of PNG files using PIL (pip install pillow)
# Run it like this: python convert_gifs.py mygif.gif mygif
import sys
from PIL import Image, ImageSequence
import os
# make a new directory for the files
try:
os.mkdir(sys.argv[2])
except OSError:
print("Can't create directory (maybe it already exists?)")
pass
with Image.open(sys.argv[1]) as im:
index = 1
for frame in ImageSequence.Iterator(im):
# resize to 128 x 128 pixels
frame.thumbnail((128, 128))
frame.save(f"{sys.argv[2]}/{sys.argv[2]}-{index}.png", "PNG", optimize=True)
index += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment