-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeGif.py
66 lines (53 loc) · 1.62 KB
/
makeGif.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
# Tom Arrell
# Animated Face Gif Creator
# run:
# python makeGif.py './MySquareImage.png'
import sys
from PIL import Image
import StringIO
import math as Math
FRAMES = 36
DEGREES = 10
SIZE = 32
# Make the initial image a square. Find the smallest size
# and crop to it to a square based on that from the top left corner.
def cropToSquare(image):
minDimension = min(image.size)
return image.crop((0, 0, minDimension, minDimension))
# Find the max edge width of a diamond that can fit in the square
# as it rotates.
def maxDiamondWidth(image):
width, height = image.size
if (width != height):
raise Exception('Image is not square')
return Math.floor(Math.sqrt((width / 2)**2 + (width / 2)**2))
def calcCenterOffset(image, toCenterWidth):
imageWidth = image.size[0]
return Math.floor((imageWidth / 2) - (toCenterWidth / 2))
def main():
cliArgs = sys.argv
fileName = cliArgs[1]
imageArray = []
im = Image.open(fileName)
im = cropToSquare(im)
# Calculate diamond width and offset to center crop frame
maxWidth = maxDiamondWidth(im)
offset = calcCenterOffset(im, maxWidth)
for i in range(FRAMES):
newIm = im
newIm = newIm.rotate(DEGREES * i, resample=Image.BICUBIC)
cropped = newIm.crop(
(offset, offset, maxWidth + offset, maxWidth + offset)
).resize((SIZE, SIZE), resample=Image.BICUBIC)
buff = StringIO.StringIO()
cropped.save(buff, 'png', optimize=True)
imageArray.append(Image.open(buff))
# Write the image array to disc as a new GIF file
imageArray[0].save(
'output.gif',
save_all=True,
append_images=imageArray[1:],
duration=40,
loop=0
)
main()