Repository navigation
Expand file tree
/
Copy pathConvertToBinary.py
More file actions
59 lines (46 loc) · 2.14 KB
/
Copy pathConvertToBinary.py
File metadata and controls
59 lines (46 loc) · 2.14 KB
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
#converts a .png into a hardcoded array that the SSD1306 monochrome monitor can load and use
#this uses a Python image library called Pillow to work.
#if Pillow is not installed run the following in the Windows console:
# python3 -m pip install --upgrade pip
# python3 -m pip install --upgrade Pillow
#for more info see: https://pillow.readthedocs.io/en/stable/installation.html
#created using help from https://stackoverflow.com/questions/11064786/get-pixels-rgb-using-pil
from PIL import Image
def convertToBinary(imageNames):
outstring = "//These hardcoded values are intented to be used in the Arduino code for the SSD1306 monochrome monitor\n\n";
for imageName in imageNames:
im = Image.open(imageName)
baseName = imageName[0:-4]
macroName = baseName.upper()
width, height = im.size
rgb_im = im.convert('RGB')
textColumns = int(width / 8) #dividing it like this will make the text data appear in a somewhat visual way
outstring += "#define " + macroName + "_WIDTH " + str(width) + "\n"
outstring += "#define " + macroName + "_HEIGHT " + str(height) + "\n"
outstring += "static const unsigned char PROGMEM " + baseName + "[] = \n{ B"
byteItr = 0
newLineItr = 0
for y in range(height):
for x in range(width):
if byteItr == 8:
newLineItr += 1
if newLineItr == textColumns:
outstring += ",\n B"
newLineItr = 0
else:
outstring += ", B"
byteItr = 0
r, g, b = rgb_im.getpixel((x, y))
if r > 128:
outstring += "1"
else:
outstring += "0"
byteItr += 1
# print(byteItr) #debug
outstring += "};\n\n"
#write the out put to the file
f = open("HARDCODED_IMAGE.txt", "w")
f.write(outstring)
f.close()
#exectution is here:
convertToBinary(['test01.png','test02.png','test03.png','test04.png'])