Skip to content

Instantly share code, notes, and snippets.

@skywodd
Created November 3, 2017 12:48
Show Gist options
  • Save skywodd/8b68bd9c7af048afcedcea3fb1807966 to your computer and use it in GitHub Desktop.
Save skywodd/8b68bd9c7af048afcedcea3fb1807966 to your computer and use it in GitHub Desktop.
Resize GIF image using Python Pillow library
from PIL import Image, ImageSequence
# Output (max) size
size = 320, 240
# Open source
im = Image.open("in.gif")
# Get sequence iterator
frames = ImageSequence.Iterator(im)
# Wrap on-the-fly thumbnail generator
def thumbnails(frames):
for frame in frames:
thumbnail = frame.copy()
thumbnail.thumbnail(size, Image.ANTIALIAS)
yield thumbnail
frames = thumbnails(frames)
# Save output
om = next(frames) # Handle first frame separately
om.info = im.info # Copy sequence info
om.save("out.gif", save_all=True, append_images=list(frames))
@jTiKey
Copy link

jTiKey commented Nov 28, 2021

To loop the gif add loop=0
om.save("out.gif", save_all=True, append_images=list(frames), loop=0)

thanks for the script!

@MMeirelless
Copy link

Thank you so much! It helped a lot :)

@Aspace2create
Copy link

oh wow tysm!

@sphinx-p
Copy link

sphinx-p commented Jan 6, 2024

Thank you so much! My boss doesn't blame me now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment