-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
onion.py
53 lines (46 loc) · 1.38 KB
/
onion.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
#!/usr/bin/python
## Use `python onion.py font/*.gif` to generates `font/*.png`.
import 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, highlight = 0.25):
gif = PIL.Image.open (gif)
## Solid stick figure, plus one solid ball:
#composite = gif.convert ('RGBA')
## No stick figure, no solid ball:
composite = PIL.Image.new ('RGBA', gif.size, 'white')
## Hybrid:
#composite = gif.convert ('RGBA')
#composite.putalpha (int (highlight * 255))
for frame in frames (gif):
#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)
def main ():
if len (sys.argv) <= 1:
print 'usage: %s filename.gif ...' % sys.argv[0]
for gif in sys.argv[1:]:
png = os.path.splitext (gif) [0] + '.png'
print gif, '->', png
assert gif != png
composite (gif, png)
if __name__ == '__main__': main ()