Last active
April 8, 2021 13:46
-
-
Save jeroenboeye/3cb45c0155a8614d3b95ddea4e7564a2 to your computer and use it in GitHub Desktop.
Resize png images and create GIF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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