-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheader_maker.py
More file actions
65 lines (52 loc) · 2.52 KB
/
header_maker.py
File metadata and controls
65 lines (52 loc) · 2.52 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
60
61
62
63
64
65
import os
from PIL import Image
import numpy as np
def bmp_to_c_array(input_folder="bitmaps", output_file="bitmap.h", prefix="image", target_size=(320, 170)):
if not os.path.exists(input_folder):
print(f"Input folder '{input_folder}' not found. Creating it...")
os.makedirs(input_folder)
print(f"Please place your BMP files in the '{input_folder}' folder and run this script again.")
return
bmp_files = sorted([f for f in os.listdir(input_folder) if f.lower().endswith('.bmp')])
if not bmp_files:
print(f"No BMP files found in {input_folder}")
return
with open(output_file, 'w') as f:
f.write("#ifndef BITMAP_H\n#define BITMAP_H\n\n#include <Arduino.h>\n\n")
f.write(f"const int IMAGE_COUNT = {len(bmp_files)};\n\n")
f.write("const uint8_t* image_array[IMAGE_COUNT];\n\n")
for idx, bmp_file in enumerate(bmp_files):
file_path = os.path.join(input_folder, bmp_file)
try:
img = Image.open(file_path).convert('1')
img = img.resize(target_size, Image.LANCZOS)
width, height = img.size
pixels = np.array(img, dtype=np.uint8)
byte_data = []
bytes_per_row = (width + 7) // 8
for y in range(height):
for x_byte in range(bytes_per_row):
byte_val = 0
for bit in range(8):
x = x_byte * 8 + bit
if x < width and pixels[y, x] == 0:
byte_val |= (1 << (7 - bit))
byte_data.append(byte_val)
array_name = f"{prefix}{idx}"
f.write(f"const uint8_t {array_name}[] PROGMEM = {{\n ")
for i, byte in enumerate(byte_data):
f.write(f"0x{byte:02X}")
if i < len(byte_data) - 1:
f.write(", ")
if (i + 1) % 16 == 0:
f.write("\n ")
f.write("\n};\n\n")
except Exception as e:
print(f"Error processing {bmp_file}: {e}")
f.write("void init_bitmap_array() {\n")
for i in range(len(bmp_files)):
f.write(f" image_array[{i}] = {prefix}{i};\n")
f.write("}\n\n#endif // BITMAP_H\n")
print(f"\n✅ Done! {output_file} written with {len(bmp_files)} images resized to {target_size[0]}x{target_size[1]}.")
if __name__ == "__main__":
bmp_to_c_array()