Skip to content

Instantly share code, notes, and snippets.

@bmcguirk
Created August 2, 2018 15:03
Show Gist options
  • Save bmcguirk/c979a09d1fe7eed001816fdfdee1b72a to your computer and use it in GitHub Desktop.
Save bmcguirk/c979a09d1fe7eed001816fdfdee1b72a to your computer and use it in GitHub Desktop.
Convert images in a directory to a gif.
# Converts a bunch of images into a gif.
# Relies on Pillow and glob
from PIL import Image
import glob
# Glob to regex the right files. Returns a list.
png_files = glob.glob("*.png")
# Bulk-open selected images.
ims = [Image.open(i) for i in png_files]
# Isolate first frame, so that you don't get a black/blank first frame.
first_frame = ims[0]
# Save first frame
first_frame.save(
"test2.gif", # Output filename - Pillow infers format from file extension.
append_images=ims[1:], # Append rest of images for .gif.
save_all=True, # Required.
loop=0, # Infinite loop.
duration=3000, # How long between frames in milliseconds.
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment