Merge pull request #6061 from radarhere/getpalette

This commit is contained in:
Hugo van Kemenade 2022-02-20 22:15:13 +02:00 committed by GitHub
commit b803b7c2a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 2 deletions

View File

@ -1,3 +1,5 @@
from PIL import Image
from .helper import hopper from .helper import hopper
@ -17,3 +19,26 @@ def test_palette():
assert palette("RGBA") is None assert palette("RGBA") is None
assert palette("CMYK") is None assert palette("CMYK") is None
assert palette("YCbCr") is None assert palette("YCbCr") is None
def test_palette_rawmode():
im = Image.new("P", (1, 1))
im.putpalette((1, 2, 3))
for rawmode in ("RGB", None):
rgb = im.getpalette(rawmode)
assert rgb == [1, 2, 3]
# Convert the RGB palette to RGBA
rgba = im.getpalette("RGBA")
assert rgba == [1, 2, 3, 255]
im.putpalette((1, 2, 3, 4), "RGBA")
# Convert the RGBA palette to RGB
rgb = im.getpalette("RGB")
assert rgb == [1, 2, 3]
for rawmode in ("RGBA", None):
rgba = im.getpalette(rawmode)
assert rgba == [1, 2, 3, 4]

View File

@ -111,6 +111,13 @@ At present, the information within each block is merely returned as a dictionary
"data" entry. This will allow more useful information to be added in the future without "data" entry. This will allow more useful information to be added in the future without
breaking backwards compatibility. breaking backwards compatibility.
Added rawmode argument to Image.getpalette()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
By default, :py:meth:`~PIL.Image.Image.getpalette` returns RGB data from the palette.
A ``rawmode`` argument has been added, to allow the mode to be chosen instead. ``None``
can be used to return data in the current mode of the palette.
Other Changes Other Changes
============= =============

View File

@ -1441,19 +1441,27 @@ class Image:
self.load() self.load()
return self.im.ptr return self.im.ptr
def getpalette(self): def getpalette(self, rawmode="RGB"):
""" """
Returns the image palette as a list. Returns the image palette as a list.
:param rawmode: The mode in which to return the palette. ``None`` will
return the palette in its current mode.
.. versionadded:: 9.1.0
:returns: A list of color values [r, g, b, ...], or None if the :returns: A list of color values [r, g, b, ...], or None if the
image has no palette. image has no palette.
""" """
self.load() self.load()
try: try:
return list(self.im.getpalette()) mode = self.im.getpalettemode()
except ValueError: except ValueError:
return None # no palette return None # no palette
if rawmode is None:
rawmode = mode
return list(self.im.getpalette(mode, rawmode))
def getpixel(self, xy): def getpixel(self, xy):
""" """

View File

@ -574,6 +574,7 @@ static struct {
/* true colour */ /* true colour */
{"RGB", "RGB", 24, ImagingPackRGB}, {"RGB", "RGB", 24, ImagingPackRGB},
{"RGB", "RGBX", 32, copy4}, {"RGB", "RGBX", 32, copy4},
{"RGB", "RGBA", 32, copy4},
{"RGB", "XRGB", 32, ImagingPackXRGB}, {"RGB", "XRGB", 32, ImagingPackXRGB},
{"RGB", "BGR", 24, ImagingPackBGR}, {"RGB", "BGR", 24, ImagingPackBGR},
{"RGB", "BGRX", 32, ImagingPackBGRX}, {"RGB", "BGRX", 32, ImagingPackBGRX},