File tree 1 file changed +8
-7
lines changed
1 file changed +8
-7
lines changed Original file line number Diff line number Diff line change 26
26
from PIL import Image
27
27
28
28
def image2bin (image , output_file ):
29
+ print (f"Converting image with dimensions { image .size [0 ]} x{ image .size [1 ]} ..." )
29
30
if output_file .endswith (('.c' , '.cpp' )):
30
- f = open (output_file , 'wt' )
31
31
is_cpp = True
32
+ f = open (output_file , 'wt' )
32
33
f .write ("const uint16_t image[%d] = {\n " % (image .size [1 ] * image .size [0 ]))
33
34
else :
34
- f = open (output_file , 'wb' )
35
35
is_cpp = False
36
+ f = open (output_file , 'wb' )
36
37
pixs = image .load ()
37
38
for y in range (image .size [1 ]):
39
+ f .write (" " )
38
40
for x in range (image .size [0 ]):
39
41
R = pixs [x , y ][0 ] >> 3
40
42
G = pixs [x , y ][1 ] >> 2
41
43
B = pixs [x , y ][2 ] >> 3
42
44
rgb = (R << 11 ) | (G << 5 ) | B
45
+ if rgb == 0 : rgb = 1
43
46
if is_cpp :
44
- strHex = ' 0x{0:04X}, ' .format (rgb )
47
+ strHex = " 0x{0:04X}," .format (rgb )
45
48
f .write (strHex )
46
49
else :
47
50
f .write (struct .pack ("B" , (rgb & 0xFF )))
48
51
f .write (struct .pack ("B" , (rgb >> 8 ) & 0xFF ))
49
- if is_cpp :
50
- f .write ("\n " )
51
- if is_cpp :
52
- f .write ("};\n " )
52
+ if is_cpp : f .write ("\n " )
53
+ if is_cpp : f .write ("};\n " )
53
54
f .close ()
54
55
55
56
if len (sys .argv ) <= 2 :
You canβt perform that action at this time.
0 commit comments