Skip to content

Commit

Permalink
lv_img_conv_py: first working version
Browse files Browse the repository at this point in the history
  • Loading branch information
NeroBurner committed Sep 18, 2023
1 parent e983549 commit 78edb9b
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions img/lv_img_conv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import argparse
import pathlib
import sys
from PIL import Image

parser = argparse.ArgumentParser()

Expand Down Expand Up @@ -36,9 +37,9 @@
# --dither, -d enable dither [boolean]
args = parser.parse_args()

img = pathlib.Path(args.img)
img_path = pathlib.Path(args.img)
out = pathlib.Path(args.output_file)
if not img.is_file():
if not img_path.is_file():
print(f"Input file is missing: '{args.img}'")
sys.exit(1)
if out.exists():
Expand All @@ -48,3 +49,55 @@
print(f"output-file exists, set --force to allow overwriting of file")
sys.exit(0)
out.touch()

# only implemented the bare minimum, everything else is not implemented
if args.color_format != "CF_TRUE_COLOR_ALPHA":
raise NotImplementedError(f"args.color_format '{args.color_format}' not implemented")
if args.output_format != "bin":
raise NotImplementedError(f"args.output_format '{args.output_format}' not implemented")
if args.binary_format != "ARGB8565_RBSWAP":
raise NotImplementedError(f"args.binary_format '{args.binary_format}' not implemented")

img = Image.open(img_path)
img.convert()

def classify_pixel(value, bits):
tmp = 1 << (8 - bits)
val = round(value / tmp) * tmp
if val < 0:
val = 0
return val
img_height = img.height
img_width = img.width
buf = bytearray(4 + img_height*img_width*3) # 4bytes header + 3 bytes (21 bit) per pixel
print(f"loaded image with width x heigth: {img_width} x {img_height}")
for y in range(img_height):
for x in range(img_width):
i = 4 + (y*img_width + x)*3 # buffer-index
pixel = img.getpixel((x,y))
r_act = classify_pixel(pixel[0], 5)
g_act = classify_pixel(pixel[1], 6)
b_act = classify_pixel(pixel[2], 5)
a = pixel[3]
if r_act > 0xF8:
r_act = 0xF8;
if g_act > 0xFC:
g_act = 0xFC;
if b_act > 0xF8:
b_act = 0xF8;
c16 = ((r_act) << 8) | ((g_act) << 3) | ((b_act) >> 3) # RGR565
buf[i + 0] = (c16 >> 8) & 0xFF
buf[i + 1] = c16 & 0xFF
buf[i + 2] = a

# write header
lv_cf = 5 # CF_TRUE_COLOR_ALPHA
header_32bit = lv_cf | (img_width << 10) | (img_height << 21)
buf[0] = header_32bit & 0xFF
buf[1] = (header_32bit & 0xFF00) >> 8
buf[2] = (header_32bit & 0xFF0000) >> 16
buf[3] = (header_32bit & 0xFF000000) >> 24

# write byte buffer to file
with open(out, "wb") as f:
f.write(buf)

0 comments on commit 78edb9b

Please sign in to comment.