Allow rawmode None to return the palette in the current mode

This commit is contained in:
Andrew Murray 2022-02-17 10:35:13 +11:00
parent 852859476b
commit 6be87277f7
2 changed files with 12 additions and 7 deletions

View File

@ -25,9 +25,10 @@ def test_palette_rawmode():
im = Image.new("P", (1, 1))
im.putpalette((1, 2, 3))
rgb = im.getpalette("RGB")
assert len(rgb) == 256 * 3
assert rgb[:3] == [1, 2, 3]
for rawmode in ("RGB", None):
rgb = im.getpalette(rawmode)
assert len(rgb) == 256 * 3
assert rgb[:3] == [1, 2, 3]
# Convert the RGB palette to RGBA
rgba = im.getpalette("RGBA")
@ -41,6 +42,7 @@ def test_palette_rawmode():
assert len(rgb) == 256 * 3
assert rgb[:3] == [1, 2, 3]
rgba = im.getpalette("RGBA")
assert len(rgba) == 256 * 4
assert rgba[:4] == [1, 2, 3, 4]
for rawmode in ("RGBA", None):
rgba = im.getpalette(rawmode)
assert len(rgba) == 256 * 4
assert rgba[:4] == [1, 2, 3, 4]

View File

@ -1405,7 +1405,8 @@ class Image:
"""
Returns the image palette as a list.
:param rawmode: The mode in which to return the palette.
:param rawmode: The mode in which to return the palette. ``None`` will
return the palette in its current mode.
:returns: A list of color values [r, g, b, ...], or None if the
image has no palette.
"""
@ -1415,6 +1416,8 @@ class Image:
mode = self.im.getpalettemode()
except ValueError:
return None # no palette
if rawmode is None:
rawmode = mode
return list(self.im.getpalette(mode, rawmode))
def getpixel(self, xy):