Skip to content

Instantly share code, notes, and snippets.

@skywodd
Created November 3, 2017 12:48
Show Gist options
  • Select an option

  • Save skywodd/8b68bd9c7af048afcedcea3fb1807966 to your computer and use it in GitHub Desktop.

Select an option

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))
@MMeirelless
Copy link
Copy Markdown

Thank you so much! It helped a lot :)

@Aspace2create
Copy link
Copy Markdown

oh wow tysm!

@griffith-xx
Copy link
Copy Markdown

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