Skip to content

Commit a481af0

Browse files
committed
lv_img_conv: support other modes like 'P'
Support other image modes like `P`, which uses 8 bits per pixel and a color palette to save space. Luckily the Pillow module can do the mode conversion for us. Fixes: InfiniTimeOrg#1985
1 parent 034d83f commit a481af0

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/resources/lv_img_conv.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ def main():
101101
img = Image.open(img_path)
102102
img_height = img.height
103103
img_width = img.width
104+
if args.color_format == "CF_TRUE_COLOR_ALPHA" and img.mode != "RGBA":
105+
# support pictures stored in other formats like with a color palette 'P'
106+
# see: https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes
107+
img = img.convert(mode="RGBA")
108+
elif args.color_format == "CF_INDEXED_1_BIT" and img.mode != "L":
109+
# for CF_INDEXED_1_BIT we need just a grayscale value per pixel
110+
img = img.convert(mode="L")
104111
if args.color_format == "CF_TRUE_COLOR_ALPHA" and args.binary_format == "ARGB8888":
105112
buf = bytearray(img_height*img_width*4) # 4 bytes (32 bit) per pixel
106113
for y in range(img_height):
@@ -140,7 +147,7 @@ def main():
140147

141148
for y in range(img_height):
142149
for x in range(img_width):
143-
c, a = img.getpixel((x,y))
150+
c = img.getpixel((x,y))
144151
p = w * y + (x >> 3) + 8 # +8 for the palette
145152
buf[p] |= (c & 0x1) << (7 - (x & 0x7))
146153
# write palette information, for indexed-1-bit we need palette with two values

0 commit comments

Comments
 (0)