-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimated_gif.py
More file actions
63 lines (45 loc) · 1.6 KB
/
animated_gif.py
File metadata and controls
63 lines (45 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Name: animated_gif.py
# Purpose: Animates all PNGs in a folder as a GIF
# Author: Andy Bell - ambell@ucdavis.edu
# Created: 11/14/14
#--------------------------------
import os
import re
from images2gif import writeGif
from PIL import Image
# from http://stackoverflow.com/questions/4623446/how-do-you-sort-files-numerically
def tryint(s):
try:
return int(s)
except:
return s
def alphanum_key(s):
""" Turn a string into a list of string and number chunks.
"z23a" -> ["z", 23, "a"]
"""
return [tryint(c) for c in re.split('([0-9]+)', s)]
def sort_nicely(l):
""" Sort the given list in the way that humans expect.
"""
l.sort(key=alphanum_key)
def animated_gif(folder_with_images, gif_filename, loop_duration, size):
"""uses images2gif.py to turn all png images in a folder into an animated GIF"""
os.chdir(folder_with_images) # changes directory to the folder with the images
png_files = []
# get list of png files in folder
for fn in os.listdir(folder_with_images):
if fn.endswith('.png'):
png_files.append(fn)
sort_nicely(png_files)
print(png_files)
# number of png_files
num_pngs = len(png_files)
png_time = float(loop_duration)/ float(num_pngs)
images = [Image.open(fn) for fn in png_files]
dim = (size, size) # change sizes for the image file dimension
#for im in images:
# im.thumbnail(dim, Image.ANTIALIAS)
output_file = os.path.join(folder_with_images, gif_filename) # path for output file
writeGif(output_file, images, png_time) # writes out GIF
if __name__ == '__main__':
animated_gif()