Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python tool for converting images to c style arrays #79

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions tools/convert-image-to-c-array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/env python3
from PIL import Image
import sys
import pathlib

# Run with `./filename.py source-image.jpg`
IMAGE_PATH = pathlib.Path(sys.argv[1])
IMAGE_NAME = IMAGE_PATH.with_suffix("")
OUTPUT_PATH = IMAGE_PATH.with_suffix(".txt")


def image_to_c_array(imported_image):
"""Generator function to convert a PIL image to a C style array containing PicoSystem compatible color_t values"""
result_array = "const color_t " + str(IMAGE_NAME) + " [" + str(imported_image.width * imported_image.height) + "] = " + '\n' + "{ " + '\n' + " "

imported_image = imported_image.convert("RGBA")

for i in range(imported_image.width):
for v in range(imported_image.height):
rgb_pixel_value = imported_image.getpixel((v,i))
r = (((rgb_pixel_value[0] - 0) * (15 - 0)) / (255 - 0)) + 0
g = (((rgb_pixel_value[1] - 0) * (15 - 0)) / (255 - 0)) + 0
b = (((rgb_pixel_value[2] - 0) * (15 - 0)) / (255 - 0)) + 0
a = (((rgb_pixel_value[3] - 0) * (15 - 0)) / (255 - 0)) + 0
converted_color = (round(r) & 0xf) | ((round(a) & 0xf) << 4) | ((round(b) & 0xf) << 8) | ((round(g) & 0xf) << 12)

result_array += (str(hex(converted_color)) + ", ")

if(i < imported_image.width - 1):
result_array += '\n '

result_array += '\n' + "};"

return result_array

img = Image.open(IMAGE_PATH)
w, h = img.size
data = image_to_c_array(img)

print(f"Converted: {w}x{h} image {IMAGE_NAME} to c array")

with open(OUTPUT_PATH, "w") as f:
f.write(data)

print(f"Written to: {OUTPUT_PATH}")