-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bitmap_to_Logo.py
48 lines (39 loc) · 1.12 KB
/
Bitmap_to_Logo.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
#open image file, convert to byte array cols by rows comma separated, then save to file
#the output is not used directly, copy into sketch header file unsigned int array
from PIL import Image
# from PIL import ImageColor
#defaults
image_name = "logo_image.bmp"
data_name = "data.txt"
x_max = 48
y_max = 24
#open image
try:
source_file = Image.open(image_name, 'r')
except IOError:
print "Error: Unable to open image file"
exit()
#image can be smaller, but not larger than 48 col by 24 row
if x_max > source_file.width or y_max > source_file.height:
print "Error: Image too large"
exit()
#open data file
try:
dest_file = open(data_name, 'w') #this should never fail
except IOError:
print "Error: Unable to open data file"
exit()
#loop
for y in range(y_max):
for x in range(x_max):
#get pixel component colors
r, g, b = source_file.getpixel((x,y))
#convert pixel value to hex string
color_val = "0x0" + hex(int(r / 16))[-1] + hex(int(g / 16))[-1] + hex(int(b / 16))[-1] + "U"
#save to data file
dest_file.write(color_val.upper() + ", ")
dest_file.write('\n')
#done
dest_file.close()
source_file.close()
print "done"