forked from bertrandom/snowball-thrower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
png2c.py
77 lines (64 loc) · 2.46 KB
/
png2c.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
67
68
69
70
71
72
73
74
75
76
77
#!/bin/python
import sys, os, getopt
from PIL import Image
def main(argv):
opts, args = getopt.getopt(argv, "pshi")
previewBilevel = False
saveBilevel = False
invertColormap = False
for opt, arg in opts:
if opt == '-h':
usage()
sys.exit()
elif opt == '-p':
previewBilevel = True
elif opt == '-s':
saveBilevel = True
elif opt == '-i':
invertColormap = True
im = Image.open(args[0]) # import 320x120 png
if not (im.size[0] == 320 and im.size[1] == 120):
print("ERROR: Image must be 320px by 120px!")
sys.exit()
im = im.convert("1") # convert to bilevel image
# dithering if necessary
if previewBilevel:
im.show()
if saveBilevel:
im.save("bilevel_" + args[0])
print("Bilevel version of " + args[0] + " saved as bilevel_" + args[0])
if not (previewBilevel or saveBilevel):
im_px = im.load()
data = []
for i in range(0,120): # iterate over the columns
for j in range(0,320): # and convert 255 vals to 0 to match logic in Joystick.c and invertColormap option
data.append(0 if im_px[j,i] == 255 else 1)
str_out = "#include <stdint.h>\n#include <avr/pgmspace.h>\n\nconst uint8_t image_data[0x12c1] PROGMEM = {"
for i in range(0, (320*120) / 8):
val = 0;
for j in range(0, 8):
val |= data[(i * 8) + j] << j
if (invertColormap):
val = ~val & 0xFF;
else:
val = val & 0xFF;
str_out += hex(val) + ", " # append hexidecimal bytes
# to the output .c array
str_out += "0x0};\n" # of bytes
with open('image.c', 'w') as f: # save output into image.c
f.write(str_out)
if (invertColormap):
print("{} converted with inverted colormap and saved to image.c".format(args[0]))
else:
print("{} converted with original colormap and saved to image.c".format(args[0]))
def usage():
print("To convert to image.c: png2c.py <yourImage.png>")
print("To convert to an inverted image.c: png2c.py -i <yourImage.png>")
print("To preview bilevel image: png2c.py -p <yourImage.png>")
print("To save bilevel image: png2c.py -s <yourImage.png>")
if __name__ == "__main__":
if len(sys.argv[1:]) == 0:
usage()
sys.exit
else:
main(sys.argv[1:])