-
Notifications
You must be signed in to change notification settings - Fork 1
/
bmp2arr.py
49 lines (32 loc) · 1.08 KB
/
bmp2arr.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
# partially stolen from https://github.com/weewStack/Python-projects/blob/master/000-BMP-Converter/n5110_BMP_converter.py
# will convert a given bitmap into an array usable in the atlas
import sys
import struct
file_name = sys.argv[1]
file_name = file_name.split(".")[0]
with open(f'{file_name}.bmp', 'rb') as bmp:
bmp.seek(10, 0)
offset = struct.unpack('I', bmp.read(4))[0]
bmp.seek(18, 0)
bmp_w = struct.unpack('I', bmp.read(4))[0]
bmp_h = struct.unpack('I', bmp.read(4))[0]
bmp.seek(34, 0)
bmp_s = struct.unpack('I', bmp.read(4))[0]
bmp_b = int(bmp_s/bmp_h)
bmp.seek(offset, 0)
bmp_line = ''
bmp_list = []
bmp_list_v = []
out = ""
for line in range(bmp_h):
for byte in range(bmp_b):
bmp_byte = bmp.read(1)
bits = format(255-struct.unpack('B', bmp_byte)[0], "08b")
bmp_line += bits
bmp_list.append(bmp_line[:bmp_w])
bmp_list_v.append(bmp_line[:bmp_w])
bmp_line = ''
bmp_list_v.reverse()
for line in bmp_list_v:
out += line
print(f"\"{out}\"")