"""Convert a PNG with transparency into an LVGL 8 image C file (LV_IMG_CF_TRUE_COLOR_ALPHA). Usage: python tools/png2lvgl.py assets/needle.png src/assets/needle_img.c needle_img Pixel format matches this firmware's lv_conf.h: LV_COLOR_DEPTH 16, LV_COLOR_16_SWAP 0, so every pixel is RGB565 little-endian followed by one alpha byte (3 bytes per pixel). Requires Pillow (python -m pip install --user pillow). """ import sys from PIL import Image src, dst, name = sys.argv[1], sys.argv[2], sys.argv[3] im = Image.open(src).convert("RGBA") w, h = im.size px = im.load() data = bytearray() for y in range(h): for x in range(w): r, g, b, a = px[x, y] c = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3) data += bytes((c & 0xFF, c >> 8, a)) lines = [] for i in range(0, len(data), 24): lines.append(" " + ", ".join("0x%02X" % v for v in data[i:i + 24]) + ",") with open(dst, "w", encoding="utf-8", newline="\n") as f: f.write(f"""/* * {name}: generated by tools/png2lvgl.py from {src.replace(chr(92), '/')} ({w}x{h}). * LV_IMG_CF_TRUE_COLOR_ALPHA, RGB565 little-endian + alpha, 3 bytes per pixel. * Regenerate after editing the PNG; do not edit by hand. */ #include #ifndef LV_ATTRIBUTE_MEM_ALIGN #define LV_ATTRIBUTE_MEM_ALIGN #endif static const LV_ATTRIBUTE_MEM_ALIGN uint8_t {name}_map[] = {{ {chr(10).join(lines)} }}; const lv_img_dsc_t {name} = {{ .header = {{ .cf = LV_IMG_CF_TRUE_COLOR_ALPHA, .always_zero = 0, .reserved = 0, .w = {w}, .h = {h}, }}, .data_size = {len(data)}, .data = {name}_map, }}; """) print(f"{dst}: {w}x{h}, {len(data)} bytes")