-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multionion.py
66 lines (58 loc) · 1.87 KB
/
multionion.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
#!/usr/bin/python
## Generates a sequence of onioned frames illustrating the animation.
from __future__ import division
import math, os, sys
import PIL.Image
def frames (image):
frame = 0
try:
while True:
image.seek (frame)
yield image
frame += 1
except EOFError:
return
def composite (gif, out, onion = 0.1, multi = 6):
gif = PIL.Image.open (gif)
n = len (list (frames (gif)))
allframes = frames (gif)
for batch in range (multi):
start = int (math.ceil (n * batch / multi))
finish = int (math.ceil (n * (batch + 1) / multi))
## Solid stick figure, plus one solid ball:
composite = allframes.next ().convert ('RGBA')
## No stick figure, no solid ball:
#composite = PIL.Image.new ('RGBA', allframes[start].size, 'white')
## Hybrid:
#composite = allframes[start].convert ('RGBA')
#composite.putalpha (int (highlight * 255))
for i in range (start+1, finish):
frame = allframes.next ()
#print frame
palette = frame.getpalette ()
def keep (x):
r, g, b = palette[3*x], palette[3*x+1], palette[3*x+2]
if r == 255 and g < 255 and b < 255:
return int (255 * onion)
else:
return 0
alpha = frame.point (keep, 'L')
frame = frame.convert ('RGBA')
frame.putalpha (alpha)
composite = PIL.Image.alpha_composite (composite, frame)
composite = composite.convert ('RGB') #.convert ('L', (1.0, 0.0, 0.0, 0.0))
composite.save (out % (multi - batch))
def main ():
if len (sys.argv) <= 1:
print 'usage: %s n filename.gif ...' % sys.argv[0]
n = int (sys.argv[1])
if n >= 10:
spec = '%02d'
else:
spec = '%d'
for gif in sys.argv[2:]:
png = os.path.splitext (gif) [0] + '-' + str (n) + '-' + spec + '.png'
print gif, '->', png
assert gif != png
composite (gif, png, multi = n)
if __name__ == '__main__': main ()