-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_to_bitmap.py
More file actions
32 lines (28 loc) · 976 Bytes
/
image_to_bitmap.py
File metadata and controls
32 lines (28 loc) · 976 Bytes
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
from PIL import Image
import numpy as np
import sys
def convert_to_bitmap(image_path, output_path):
# Open and resize image
img = Image.open(image_path)
img = img.convert('RGB')
img = img.resize((96, 64), Image.Resampling.LANCZOS)
# Convert to RGB565 format
pixels = np.array(img)
r = (pixels[:,:,0] >> 3).astype(np.uint16) << 11
g = (pixels[:,:,1] >> 2).astype(np.uint16) << 5
b = (pixels[:,:,2] >> 3).astype(np.uint16)
rgb565 = r | g | b
# Create C array
with open(output_path, 'w') as f:
f.write('const uint16_t image_data[] = {\n')
for y in range(64):
f.write(' ')
for x in range(96):
f.write(f'0x{rgb565[y,x]:04X}, ')
f.write('\n')
f.write('};\n')
if __name__ == '__main__':
if len(sys.argv) != 3:
print("Usage: python script.py input_image output.h")
sys.exit(1)
convert_to_bitmap(sys.argv[1], sys.argv[2])