-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmp2hex.py
52 lines (49 loc) · 1.95 KB
/
bmp2hex.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
from matplotlib.image import imread
from PIL import Image
import os
# read all the file from image folder
dirs = os.listdir('./images/')
for files in dirs:
file = './images/'+files;
base = os.path.basename(file)
fileName, fileType = os.path.splitext(base);
# select bmp file
if fileType == ".bmp":
rgb = imread(file)
img = rgb[:,:,0]
width = img.shape[0]
heigth = img.shape[1]
cfile = open('./images/'+fileName + ".c","w+")
hfile = open('./images/'+fileName + ".h","w+")
hfile.write("#ifndef _"+fileName.upper()+"_INCLUDED_\n #define _"+fileName.upper()+"_INCLUDED_\n extern flash unsigned char "+fileName+"[];\n#endif")
hfile.close()
# define var that keep hex of bmp image
string = "flash unsigned char "+fileName+"[]={\n"+hex(heigth)+", 0x00,\n"+hex(width)+", 0x00, \n#ifndef _GLCD_DATA_BYTEY_\n"
counter = 0
# horizental row
for i in range(width):
for j in range(int(heigth/8)):
counter += 1
decNumber = 0
for bin in range(8):
if(img[i][j * 8 + bin] == 0):
decNumber += pow(2, bin)
string += str(format(decNumber, "#04x")) + ", "
if counter % 8 == 0:
string += "\n"
string += "#else\n"
# vertical row
for i in range(int(width/8)):
for j in range(heigth):
counter += 1
decNumber = 0
for bin in range(8):
if(img[i * 8 + bin][j] == 0):
decNumber += pow(2, bin)
string += str(format(decNumber, "#04x")) + ", "
if counter % 8 == 0:
string += "\n"
string += "#endif\n};"
# write var string in c file
cfile.write(string)
cfile.close()