-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakegifs.py
80 lines (65 loc) · 2.63 KB
/
makegifs.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# ----------------------------------------------------------------------------------
# | Settings
# ----------------------------------------------------------------------------------
fileTypesToGif = ["jpg", "jpeg", "png", "tiff", "webp"]
naturalSort = True # numbers will be sorted naturally (1,2,3,4,5,6,7,8,9,10,11) and not computer bullshit (1,10,2,3,etc.)
framesPerSecond = 15
exportLocation = "./" # parent directory
# ----------------------------------------------------------------------------------
# | Code
# ----------------------------------------------------------------------------------
import os
import string
from PIL import Image
from natsort import natsorted
from images2gif import writeGif
from collections import defaultdict
print
print
print
print ("----------------------------------------")
print (" Start")
print ("----------------------------------------")
rootDir = '.' # Set the directory you want to start from
counter = 0
for dirName, subdirList, fileList in os.walk(rootDir):
print
print ("Folder: " + dirName)
gifName = os.path.split( os.path.normpath(dirName) )[1]
if gifName == ".":
gifName = "unnamed" + str(counter)
gifName += ".gif"
# Sort files
sortedFileList = fileList
if naturalSort:
sortedFileList = natsorted(sortedFileList)
images = []
for fileName in sortedFileList:
fileNameSplitByPeriod = fileName.split(".") #image.jpg -> ['image', 'jpg']
extension = fileNameSplitByPeriod[len(fileNameSplitByPeriod)-1].lower() # image.jpg -> 'jpg'
if extension in fileTypesToGif:
fullPath = os.path.abspath( dirName + "/"+ fileName) #path to file including filename
images.append(fullPath)
print "Images found: " + str(images.__len__())
if images.__len__() > 1:
numberOfFrames = images.__len__()
duration = float(numberOfFrames) / framesPerSecond
durationPerFrame = 1/float(framesPerSecond)
print gifName + ' (' + str(numberOfFrames) + ' frames -> ' + str(duration) + 's)'
imgs = []
for x in xrange(0, images.__len__() - 1):
img = Image.open(images[x])
imgCopy = img.copy() # Necessary due to Pillow issue https://github.com/python-pillow/Pillow/issues/1237
imgs.append(imgCopy)
img.close()
writeGif(exportLocation + gifName, imgs, durationPerFrame)
counter += 1
else:
print "Not enough images to create animation (needs more than two)"
print
print ("----------------------------------------")
print (" All done - glad I could help :)")
print ("----------------------------------------")
print
print
print