Skip to content

Commit 3d7ac16

Browse files
committed
πŸ§‘β€πŸ’» image2bin --transparency flag
1 parent 68a2459 commit 3d7ac16

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

β€Žbuildroot/share/scripts/gen-tft-image.py

100644100755
+22-12
Original file line numberDiff line numberDiff line change
@@ -25,41 +25,51 @@
2525
import sys, struct
2626
from PIL import Image
2727

28-
def image2bin(image, output_file):
29-
print(f"Converting image with dimensions {image.size[0]}x{image.size[1]}...")
28+
def image2bin(image, output_file, transparency):
29+
w, h = image.size[0], image.size[1]
30+
print(f"Converting image with dimensions {w}x{h}...")
3031
if output_file.endswith(('.c', '.cpp')):
3132
is_cpp = True
33+
row_sp, item_sp = (" ", "") if w >= 480 else (" ", " ")
34+
row_end, data_end = "\n", "};\n"
3235
f = open(output_file, 'wt')
33-
f.write("const uint16_t image[%d] = {\n" % (image.size[1] * image.size[0]))
36+
f.write("const uint16_t image[%d] = {\n" % (h * w))
3437
else:
3538
is_cpp = False
39+
row_sp, row_end, data_end = b"", b"", b""
3640
f = open(output_file, 'wb')
41+
tcolor, got_tcolor = 0, False
3742
pixs = image.load()
38-
for y in range(image.size[1]):
39-
f.write(" ")
40-
for x in range(image.size[0]):
43+
for y in range(h):
44+
f.write(row_sp)
45+
for x in range(w):
4146
R = pixs[x, y][0] >> 3
4247
G = pixs[x, y][1] >> 2
4348
B = pixs[x, y][2] >> 3
4449
rgb = (R << 11) | (G << 5) | B
45-
if rgb == 0: rgb = 1
50+
if transparency:
51+
if not got_tcolor:
52+
got_tcolor = True
53+
tcolor = rgb # First pixel color is transparent
54+
if rgb == tcolor: rgb = 1 # "color 1" is transparent
4655
if is_cpp:
47-
strHex = " 0x{0:04X},".format(rgb)
56+
strHex = item_sp + "0x{0:04X},".format(rgb)
4857
f.write(strHex)
4958
else:
5059
f.write(struct.pack("B", (rgb & 0xFF)))
5160
f.write(struct.pack("B", (rgb >> 8) & 0xFF))
52-
if is_cpp: f.write("\n")
53-
if is_cpp: f.write("};\n")
61+
f.write(row_end)
62+
f.write(data_end)
5463
f.close()
5564

5665
if len(sys.argv) <= 2:
5766
print("Utility to export a image in Marlin TFT friendly format.")
5867
print("It will dump a raw bin RGB565 image or create a CPP file with an array of 16 bit image pixels.")
59-
print("Usage: gen-tft-image.py INPUT_IMAGE.(png|bmp|jpg) OUTPUT_FILE.(cpp|bin)")
68+
print("Usage: gen-tft-image.py INPUT_IMAGE.(png|bmp|jpg) OUTPUT_FILE.(cpp|bin) [--transparency]")
6069
print("Author: rhapsodyv")
6170
exit(1)
6271

72+
transparency = len(sys.argv) > 3 and sys.argv[3] == "--transparency"
6373
output_img = sys.argv[2]
6474
img = Image.open(sys.argv[1])
65-
image2bin(img, output_img)
75+
image2bin(img, output_img, transparency)

0 commit comments

Comments
Β (0)