-
Notifications
You must be signed in to change notification settings - Fork 3
/
util_gif2pic.py
51 lines (37 loc) · 908 Bytes
/
util_gif2pic.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
import sys
from PIL import Image
from format_pic import Pic
infile = sys.argv[1]
outfile = sys.argv[2] if len(sys.argv)>2 else (infile + '.pic')
del_palette = 16
im = Image.open(infile)
w = im.width
h = im.height
print w, h
pic = Pic()
pic.width = w
pic.height = h
# convert palette
im_pal = im.getpalette()
pal = []
for i in range(0, len(im_pal), 3):
pal.append((im_pal[i+0], im_pal[i + 1], im_pal[i + 2]))
print len(pal)
for i in range(0, del_palette):
pal[i] = (255, 0, 255)
# crop palette
p = 0
while p < len(pal) and pal[p][0] == 255 and pal[p][1] == 0 and pal[p][2] == 255:
pal[p] = None
p += 1
p = len(pal) - 1
while p > 0 and pal[p][0] == 255 and pal[p][1] == 0 and pal[p][2] == 255:
pal[p] = None
p -= 1
print pal
pic.pal = pal
# convert img data
im_data = list(im.getdata())
for y in range(0, h):
pic.pic.append(im_data[y*w:y*w+w])
pic.write_file(outfile)