diff --git a/buildroot/share/scripts/gen-tft-image.py b/buildroot/share/scripts/gen-tft-image.py old mode 100644 new mode 100755 index 95471a991286..8b807800a8b9 --- a/buildroot/share/scripts/gen-tft-image.py +++ b/buildroot/share/scripts/gen-tft-image.py @@ -25,41 +25,51 @@ import sys, struct from PIL import Image -def image2bin(image, output_file): - print(f"Converting image with dimensions {image.size[0]}x{image.size[1]}...") +def image2bin(image, output_file, transparency): + w, h = image.size[0], image.size[1] + print(f"Converting image with dimensions {w}x{h}...") if output_file.endswith(('.c', '.cpp')): is_cpp = True + row_sp, item_sp = (" ", "") if w >= 480 else (" ", " ") + row_end, data_end = "\n", "};\n" f = open(output_file, 'wt') - f.write("const uint16_t image[%d] = {\n" % (image.size[1] * image.size[0])) + f.write("const uint16_t image[%d] = {\n" % (h * w)) else: is_cpp = False + row_sp, row_end, data_end = b"", b"", b"" f = open(output_file, 'wb') + tcolor, got_tcolor = 0, False pixs = image.load() - for y in range(image.size[1]): - f.write(" ") - for x in range(image.size[0]): + for y in range(h): + f.write(row_sp) + for x in range(w): R = pixs[x, y][0] >> 3 G = pixs[x, y][1] >> 2 B = pixs[x, y][2] >> 3 rgb = (R << 11) | (G << 5) | B - if rgb == 0: rgb = 1 + if transparency: + if not got_tcolor: + got_tcolor = True + tcolor = rgb # First pixel color is transparent + if rgb == tcolor: rgb = 1 # "color 1" is transparent if is_cpp: - strHex = " 0x{0:04X},".format(rgb) + strHex = item_sp + "0x{0:04X},".format(rgb) f.write(strHex) else: f.write(struct.pack("B", (rgb & 0xFF))) f.write(struct.pack("B", (rgb >> 8) & 0xFF)) - if is_cpp: f.write("\n") - if is_cpp: f.write("};\n") + f.write(row_end) + f.write(data_end) f.close() if len(sys.argv) <= 2: print("Utility to export a image in Marlin TFT friendly format.") print("It will dump a raw bin RGB565 image or create a CPP file with an array of 16 bit image pixels.") - print("Usage: gen-tft-image.py INPUT_IMAGE.(png|bmp|jpg) OUTPUT_FILE.(cpp|bin)") + print("Usage: gen-tft-image.py INPUT_IMAGE.(png|bmp|jpg) OUTPUT_FILE.(cpp|bin) [--transparency]") print("Author: rhapsodyv") exit(1) +transparency = len(sys.argv) > 3 and sys.argv[3] == "--transparency" output_img = sys.argv[2] img = Image.open(sys.argv[1]) -image2bin(img, output_img) +image2bin(img, output_img, transparency)