-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimages_to_lvgl.py
128 lines (101 loc) · 4.49 KB
/
images_to_lvgl.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# pip install Pillow
import io
import glob
import os
from pickletools import optimize
import sys
import hashlib
from glob import glob
from turtle import width
from PIL import Image
if (len(sys.argv) <= 2):
print('Usage: images_to_lvgl.py <input_files> raw/png/gif <file.h> <maxwidth> <maxheight>')
sys.exit(1)
files = glob(sys.argv[1])
type = sys.argv[2]
file_h = sys.argv[3]
resize = None
if (len(sys.argv) >= 5):
resize= (int(sys.argv[4]), int(sys.argv[5]))
output_file = open(file_h, 'w')
output_file.write(
'//*******************************************************************************\n'
f'// Type: LVGL / {type}\n')
if (resize != None):
output_file.write('// Resize: ' + str(resize[0]) + 'x' + str(resize[1]) + '\n')
output_file.write(
'// ******************************************************************************\n\n'
'#if defined(LV_LVGL_H_INCLUDE_SIMPLE)\n'
'#include "lvgl.h"\n'
'#else\n'
'#include "lvgl/lvgl.h"\n'
'#endif\n\n')
hashes = {}
duplicates = {}
for file_name in files:
print(f'Processing: {file_name}... ')
image = Image.open(file_name)
if (resize != None):
aspectRatio = image.size[0] / image.size[1]
if (aspectRatio > 1):
size = (resize[0], int(resize[0] / aspectRatio))
else:
size = (int(resize[1] * aspectRatio), resize[1])
image = image.resize(size)
else:
size = image.size
image = image.convert('RGBA')
hash = hashlib.md5(image.tobytes()).hexdigest()
base_name = os.path.splitext(os.path.basename(file_name))[0]
if (not hash in hashes):
hashes[hash]= base_name
output_file.write('\n')
output_file.write('constexpr unsigned char ' + base_name + '_map[] = { \n')
if (type == 'raw'):
convert_RGB888_to_RGB332 = lambda rgb: (rgb[0] >> 5) << 5 | (rgb[1] >> 6) << 2 | (rgb[2] >> 6)
convert_RGB888_to_RGB565 = lambda rgb: (rgb[0] >> 3) << 11 | (rgb[1] >> 2) << 5 | (rgb[2] >> 3)
output_file.write('#if LV_COLOR_DEPTH == 1 || LV_COLOR_DEPTH == 8\n'
'// Pixel format: RGB332 Red: 3 bit, Green: 3 bit, Blue: 2 bit\n')
output_file.write(', '.join(f'0x{(i):02x}' for i in map(convert_RGB888_to_RGB332, image.getdata())))
output_file.write('\n#endif\n')
output_file.write('#if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP == 0\n'
'// Pixel format: RGB565 Red: 5 bit, Green: 6 bit, Blue: 5 bit\n')
output_file.write(', '.join(f'0x{(i & 0xff):02x},0x{(i >> 8):02x}' for i in map(convert_RGB888_to_RGB565, image.getdata())))
output_file.write('\n#endif\n')
output_file.write('#if LV_COLOR_DEPTH == 16 && LV_COLOR_16_SWAP != 0\n'
'// Pixel format: RGB565 Red: 5 bit, Green: 6 bit, Blue: 5 bit BUT the 2 bytes are swapped\n')
output_file.write(', '.join(f'0x{(i >> 8):02x},0x{(i & 0xff):02x}' for i in map(convert_RGB888_to_RGB565, image.getdata())))
output_file.write('\n#endif\n')
output_file.write('#if LV_COLOR_DEPTH == 32\n'
'// Pixel format: RGBA8888 Red: 8 bit, Green: 8 bit, Blue: 8 bit, Alpha: 8 bit \n')
output_file.write(', '.join(f'0x{i[0]:02x},0x{(i[1]):02x},0x{(i[2]):02x},0x{(i[3]):02x}' for i in image.getdata()))
output_file.write('\n#endif\n')
else:
byte_io = io.BytesIO()
image.save(byte_io, optimize=True, format=type)
output_file.write(f'// {type} Data\n')
output_file.write(','.join(f'0x{(i):02x}' for i in byte_io.getvalue()))
output_file.write('\n')
output_file.write('};\n')
output_file.write(
f'const lv_img_dsc_t {base_name} = {{\n'
' .header = {\n');
if (type == 'raw'):
output_file.write(' .cf = LV_IMG_CF_TRUE_COLOR,\n')
else:
output_file.write(' .cf = LV_IMG_SRC_VARIABLE,\n')
output_file.write(
' .always_zero = 0,\n'
f' .w = {image.width},\n'
f' .h = {image.height}}},\n'
f' .data_size = sizeof({base_name + "_map"}),\n'
f' .data = {base_name + "_map"}}};\n')
else:
duplicates[base_name] = hashes[hash]
print(f'Duplicate: {hashes[hash]}')
image.close()
output_file.write('\n\n')
for mapping in duplicates:
output_file.write('#define ' + mapping + ' ' + duplicates[mapping] + '\n')
output_file.close()
print('Done.')