Skip to content

Instantly share code, notes, and snippets.

@jeroenboeye
Last active April 8, 2021 13:46
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 jeroenboeye/3cb45c0155a8614d3b95ddea4e7564a2 to your computer and use it in GitHub Desktop.
Save jeroenboeye/3cb45c0155a8614d3b95ddea4e7564a2 to your computer and use it in GitHub Desktop.
Resize png images and create GIF
import glob
from PIL import Image
# Measure current aspect ratio beforehand
width = 700 # desired width
aspect_ratio = 0.64267 # original aspect ratio (width / height)
height = int(width / aspect_ratio) # derived new height
# Read png images from plots folder, resize them and add to list.
# First list element is saved as img, rest as imgs (list)
# Plot names should be created so that order is correct after sorting
img, *imgs = [Image.open(f).resize((width, height), Image.ANTIALIAS) for f in sorted(glob.glob("plots/*.png"))]
img.save(fp='output.gif', format='GIF', append_images=imgs, save_all=True, duration=400, loop=0)
# Optional optimization of GIF size
# from pygifsicle import optimize
# optimize('output.gif')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment