-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
24 lines (19 loc) · 796 Bytes
/
main.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
from PIL import Image
import io
def image_to_byte_array(image_path):
with Image.open(image_path) as img:
with io.BytesIO() as byte_io:
img.save(byte_io, format='PNG')
return byte_io.getvalue()
def save_to_cpp_file(byte_array, filename):
size = len(byte_array)
with open(filename, 'w') as f:
f.write(f'inline unsigned char image_data[{size}] = {{\n')
formatted_bytes = ', '.join(f'0x{b:02X}' for b in byte_array)
f.write(f' {formatted_bytes},\n')
f.write(f'}};\n')
image_path = input("Enter the image path: ")
output_file = input("Enter the output cpp file path: ")
byte_array = image_to_byte_array(image_path)
save_to_cpp_file(byte_array, output_file)
print(f"The byte array has been saved to '{output_file}'.")