-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimg2rle.py
65 lines (56 loc) · 1.61 KB
/
img2rle.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
from PIL import Image
def RLEencode(sequence):
count = 1
result = []
for x,item in enumerate(sequence):
if x == 0:
continue
elif item == sequence[x - 1]:
count += 1
else:
result.append((sequence[x - 1], count))
count = 1
result.append((sequence[len(sequence) - 1], count))
return result
def MergeByte(sequence):
result = []
for item in sequence:
colorbit=int(item[0])
lenbit=item[1]
tmp = (colorbit & 0xffff) << 14 # 两位颜色位,支持四色,占用开头 11000000 00000000
tmp = tmp | lenbit # 连续长度
result.append(tmp)
return result
img = Image.open('BootScreen1.png')
rgb_im = img.convert('RGB')
pixels=[]
ColorTab=[]
for x in range(img.height):
for y in range(img.width):
r, g, b = rgb_im.getpixel((y, x))
hasColorTab = False
for idx in range(len(ColorTab)):
ir,ig,ib = ColorTab[idx]
if ir == r and ig == g and ib == b:
pixels.append(str(idx))
hasColorTab = True
break
if not hasColorTab:
pixels.append(str(len(ColorTab)))
ColorTab.append((r,g,b))
img.close()
ORLEenc = RLEencode(pixels)
ORLEenc = MergeByte(ORLEenc)
print("{")
count =0
for x in ORLEenc:
count +=1
print("0x{:04x}, ".format(x),end = '')
if count == 8:
print()
count=0
print("}")
print("Original size:%d Bytes"%len(pixels))
print("Original RLE encode size:%d(%d Bytes)"%(len(ORLEenc),len(ORLEenc)*2))
print("colortab:")
print(ColorTab)