Skip to content

Commit

Permalink
Fix python-pillow#6652: Handle translucent color used in RGB ImagePal…
Browse files Browse the repository at this point in the history
…lete
  • Loading branch information
jsbueno committed Oct 9, 2022
1 parent 243402e commit f9a3178
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 9 additions & 0 deletions Tests/test_imagepalette.py
Expand Up @@ -50,6 +50,15 @@ def test_getcolor():
palette.getcolor("unknown")


def test_getcolor_raises_on_incompatible_color():
palette = ImagePalette.ImagePalette(mode="RGB")
# Opaque RGBA colors should work
palette.getcolor((0, 0, 0, 255))
assert palette.getcolor((0, 0, 0)) == palette.getcolor((0, 0, 0, 255))
with pytest.raises(ValueError):
palette.getcolor((0, 0, 0, 128))


@pytest.mark.parametrize(
"index, palette",
[
Expand Down
8 changes: 6 additions & 2 deletions src/PIL/ImagePalette.py
Expand Up @@ -114,9 +114,13 @@ def getcolor(self, color, image=None):
if self.rawmode:
raise ValueError("palette contains raw palette data")
if isinstance(color, tuple):
if self.mode == "RGB":
if len(color) == 4 and color[3] == 255:
if self.mode == "RGB" and len(color) == 4:
if color[3] == 255:
color = color[:3]
else:
raise ValueError(
"RGB ImagePalette can't handle non-opaque RGBA colors"
)
elif self.mode == "RGBA":
if len(color) == 3:
color += (255,)
Expand Down

0 comments on commit f9a3178

Please sign in to comment.