Skip to content

Instantly share code, notes, and snippets.

@danclewley
Created August 1, 2015 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save danclewley/eda599998c9541bdb154 to your computer and use it in GitHub Desktop.
Save danclewley/eda599998c9541bdb154 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
A script to create a GIF from a sequence of images using
ImageMagick.
Applies natural sorting to file names.
Dan Clewley (http://spectraldifferences.wordpress.com)
"""
import argparse
import re
import subprocess
re_natural = re.compile('[0-9]+|[^0-9]+')
def natural_key(s):
"""
Natural sorting function from: http://stackoverflow.com/questions/12184015/in-python-how-can-i-naturally-sort-a-list-of-alphanumeric-strings-such-that-alp
"""
return [(1, int(c)) if c.isdigit() else (0, c.lower()) for c in re_natural.findall(s)] + [s]
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Create an animated gif from a series of images')
parser.add_argument("images", nargs='+',type=str, help="Input images")
parser.add_argument('-o', '--outgif',
metavar ='Output gif',
help ='Output name for gif',
required=True)
args=parser.parse_args()
sorted_png_files = sorted(args.images, key=natural_key)
# Call ImageMagick convert command
convert_cmd = ['convert','-delay','20','-loop','0']
convert_cmd.extend(sorted_png_files)
convert_cmd.extend([args.outgif])
subprocess.call(convert_cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment