Skip to content

Instantly share code, notes, and snippets.

@shinshin86
Last active June 1, 2022 21:20
Show Gist options
  • Save shinshin86/475a1850a86a4c1d6cbfacdbc9fab8e7 to your computer and use it in GitHub Desktop.
Save shinshin86/475a1850a86a4c1d6cbfacdbc9fab8e7 to your computer and use it in GitHub Desktop.
Python script to join images together to create a GIF.
from PIL import Image
from os import path
from glob import glob
import sys
##################
# Usage:
# python make_git.py <input_dir_name> <output_filename>
##################
##################
### Parameter
# Maximum number of images to be used (because a large number of images will result in an error)
IMAGE_COUNT_LIMIT = 200
# This is the display time for a single image. Lower the number, the smoother (finer) the GIF will be displayed.
DURATION = 100
##################
def create_gif(input_dir, output_filename):
fp_list = sorted(glob(path.join(*[input_dir, '*'])))
images = []
for i, fp in enumerate(fp_list):
images.append(Image.open(fp))
if(i == IMAGE_COUNT_LIMIT):
break
images[0].save(output_filename, save_all=True, append_images=images[1:], optimize=False, duration=DURATION, loop=0)
print("Successful")
if __name__ == "__main__":
args = sys.argv
usage_text = """Usage:
python make_git.py <input_dir_name> <output_filename>"""
if len(args) != 3:
print(usage_text)
sys.exit()
input_dir = args[1]
output_filename = args[2]
print("Input dir: ", input_dir)
print("Output filename: ", output_filename)
create_gif(input_dir, output_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment