This commit is contained in:
Kovid Goyal 2024-01-26 21:41:24 +05:30
parent f7d9b0383e
commit 99c0ce77f7
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -692,29 +692,29 @@ def convert_PIL_image_to_pixmap(im):
data = None data = None
colortable = None colortable = None
if im.mode == "1": if im.mode == "1":
format = QImage.Format.Format_Mono fmt = QImage.Format.Format_Mono
elif im.mode == "L": elif im.mode == "L":
format = QImage.Format.Format_Indexed8 fmt = QImage.Format.Format_Indexed8
colortable = [qRgba(i, i, i, 255) & 0xFFFFFFFF for i in range(256)] colortable = [qRgba(i, i, i, 255) & 0xFFFFFFFF for i in range(256)]
elif im.mode == "P": elif im.mode == "P":
format = QImage.Format.Format_Indexed8 fmt = QImage.Format.Format_Indexed8
palette = im.getpalette() palette = im.getpalette()
colortable = [qRgba(*palette[i : i + 3], 255) & 0xFFFFFFFF for i in range(0, len(palette), 3)] colortable = [qRgba(*palette[i : i + 3], 255) & 0xFFFFFFFF for i in range(0, len(palette), 3)]
elif im.mode == "RGB": elif im.mode == "RGB":
format = QImage.Format.Format_RGBX8888 fmt = QImage.Format.Format_RGBX8888
data = im.convert("RGBA").tobytes("raw", "RGBA") data = im.convert("RGBA").tobytes("raw", "RGBA")
elif im.mode == "RGBA": elif im.mode == "RGBA":
format = QImage.Format.Format_RGBA8888 fmt = QImage.Format.Format_RGBA8888
data = im.tobytes("raw", "RGBA") data = im.tobytes("raw", "RGBA")
elif im.mode == "I;16": elif im.mode == "I;16":
im = im.point(lambda i: i * 256) im = im.point(lambda i: i * 256)
format = QImage.Format.Format_Grayscale16 fmt = QImage.Format.Format_Grayscale16
else: else:
raise ValueError(f"unsupported image mode {repr(im.mode)}") raise ValueError(f"unsupported image mode {repr(im.mode)}")
size = im.size size = im.size
data = data or align8to32(im.tobytes(), size[0], im.mode) data = data or align8to32(im.tobytes(), size[0], im.mode)
qimg = QImage(data, size[0], size[1], format) qimg = QImage(data, size[0], size[1], fmt)
if colortable: if colortable:
qimg.setColorTable(colortable) qimg.setColorTable(colortable)
return QPixmap.fromImage(qimg) return QPixmap.fromImage(qimg)