Skip to content

Commit

Permalink
Allow rawmode None to return the palette in the current mode
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Feb 16, 2022
1 parent 8528594 commit ca8e5e1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
14 changes: 8 additions & 6 deletions Tests/test_image_getpalette.py
Expand Up @@ -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")
Expand All @@ -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]
5 changes: 4 additions & 1 deletion src/PIL/Image.py
Expand Up @@ -1405,7 +1405,8 @@ def getpalette(self, rawmode="RGB"):
"""
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.
"""
Expand All @@ -1415,6 +1416,8 @@ def getpalette(self, rawmode="RGB"):
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):
Expand Down

0 comments on commit ca8e5e1

Please sign in to comment.