Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/PIL/BmpImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/ImImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
14 changes: 8 additions & 6 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/PalmImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/PngImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
4 changes: 2 additions & 2 deletions src/PIL/TgaImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
11 changes: 3 additions & 8 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading