From 7a2470d7b7645167235d8fdfac0464dcd94b19a4 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 16 Oct 2026 09:57:09 +1100 Subject: [PATCH] Use existing palette mode for C getpalette mode --- src/PIL/BmpImagePlugin.py | 2 +- src/PIL/ImImagePlugin.py | 2 +- src/PIL/Image.py | 14 ++++++++------ src/PIL/PalmImagePlugin.py | 2 +- src/PIL/PngImagePlugin.py | 2 +- src/PIL/TgaImagePlugin.py | 4 ++-- src/PIL/TiffImagePlugin.py | 2 +- src/_imaging.c | 11 +++-------- 8 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/PIL/BmpImagePlugin.py b/src/PIL/BmpImagePlugin.py index b93ff6dddf0..24dbbea27f5 100644 --- a/src/PIL/BmpImagePlugin.py +++ b/src/PIL/BmpImagePlugin.py @@ -460,7 +460,7 @@ def _save( palette = b"".join(o8(i) * 3 + b"\x00" for i in range(256)) elif im.mode == "P": # Colors used should not be zero, as that is treated as the maximum - palette = im.im.getpalette("RGB", "BGRX") or b"\x00\x00\x00\x00" + palette = im.im.getpalette("BGRX") or b"\x00\x00\x00\x00" colors = len(palette) // 4 else: palette = None diff --git a/src/PIL/ImImagePlugin.py b/src/PIL/ImImagePlugin.py index 5cf934fee6d..4c785520361 100644 --- a/src/PIL/ImImagePlugin.py +++ b/src/PIL/ImImagePlugin.py @@ -378,7 +378,7 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: fp.write(b"Lut: 1\r\n") fp.write(b"\000" * (511 - fp.tell()) + b"\032") if im.mode in ["P", "PA"]: - im_palette = im.im.getpalette("RGB", "RGB;L") + im_palette = im.im.getpalette("RGB;L") colors = len(im_palette) // 3 palette = b"" for i in range(3): diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 6dec284d086..55fbee9869e 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -1353,7 +1353,7 @@ def quantize( from . import ImagePalette mode = im.im.getpalettemode() - palette_data = im.im.getpalette(mode)[: colors * len(mode)] + palette_data = im.im.getpalette()[: colors * len(mode)] im.palette = ImagePalette.ImagePalette(mode, palette_data) return im @@ -1707,12 +1707,14 @@ def getpalette(self, rawmode: str | None = "RGB") -> list[int] | None: self.load() try: - mode = self.im.getpalettemode() + palette = ( + self.im.getpalette(rawmode) + if rawmode is not None + else self.im.getpalette() + ) except ValueError: return None # no palette - if rawmode is None: - rawmode = mode - return list(self.im.getpalette(mode, rawmode)) + return list(palette) @property def has_transparency_data(self) -> bool: @@ -2251,7 +2253,7 @@ def remap_palette( palette_mode = self.im.getpalettemode() if palette_mode == "RGBA": bands = 4 - source_palette = self.im.getpalette(palette_mode) + source_palette = self.im.getpalette() else: # L-mode source_palette = bytearray(i // 3 for i in range(768)) elif len(source_palette) > 768: diff --git a/src/PIL/PalmImagePlugin.py b/src/PIL/PalmImagePlugin.py index d5a0116783a..8cb0844ed4f 100644 --- a/src/PIL/PalmImagePlugin.py +++ b/src/PIL/PalmImagePlugin.py @@ -174,7 +174,7 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: flags = 0 if im.mode == "P": flags |= _FLAGS["custom-colormap"] - colormap = im.im.getpalette() + colormap = im.im.getpalette("RGB") colors = len(colormap) // 3 colormapsize = 4 * colors + 2 else: diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py index f61a0bb37d7..9ce7657e78d 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -1522,7 +1522,7 @@ def _save( msg = "cannot use transparency for this mode" raise OSError(msg) elif mode == "P" and palette_frame_with_alpha: - alpha = palette_frame_with_alpha.im.getpalette("RGBA", "A") + alpha = palette_frame_with_alpha.im.getpalette("A") alpha_bytes = colors chunk(fp, b"tRNS", alpha[:alpha_bytes]) diff --git a/src/PIL/TgaImagePlugin.py b/src/PIL/TgaImagePlugin.py index 8a112d50558..70a1c2e93e9 100644 --- a/src/PIL/TgaImagePlugin.py +++ b/src/PIL/TgaImagePlugin.py @@ -227,10 +227,10 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: if colormaptype: assert im.palette is not None if im.palette.mode == "RGBA": - palette = im.im.getpalette("RGBA", "BGRA") + palette = im.im.getpalette("BGRA") colormaplength, colormapentry = len(palette) // 4, 32 else: - palette = im.im.getpalette("RGB", "BGR") + palette = im.im.getpalette("BGR") colormaplength, colormapentry = len(palette) // 3, 24 else: colormaplength, colormapentry = 0, 0 diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 35b490bd4e3..0faaab903bc 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -1882,7 +1882,7 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: im = ImageOps.invert(im) if im.mode in ["P", "PA"]: - lut = im.im.getpalette("RGB", "RGB;L") + lut = im.im.getpalette("RGB;L") colormap = [] colors = len(lut) // 3 for i in range(3): diff --git a/src/_imaging.c b/src/_imaging.c index 248c3f5578a..a2f953bdf96 100644 --- a/src/_imaging.c +++ b/src/_imaging.c @@ -1309,22 +1309,17 @@ _getpalette_impl(ImagingObject *self, PyObject *args) { int bits; ImagingShuffler pack; - char *mode_name = "RGB"; char *rawmode_name = NULL; - if (!PyArg_ParseTuple(args, "|ss", &mode_name, &rawmode_name)) { + if (!PyArg_ParseTuple(args, "|s", &rawmode_name)) { return NULL; } - if (rawmode_name == NULL) { - rawmode_name = mode_name; - } - if (!self->image->palette) { PyErr_SetString(PyExc_ValueError, no_palette); return NULL; } - const ModeID mode = findModeID(mode_name); - const RawModeID rawmode = findRawModeID(rawmode_name); + const ModeID mode = self->image->palette->mode; + const RawModeID rawmode = rawmode_name != NULL ? findRawModeID(rawmode_name) : mode; pack = ImagingFindPacker(mode, rawmode, &bits); if (!pack) {