Skip to content

Commit f9a3178

Browse files
committed
Fix python-pillow#6652: Handle translucent color used in RGB ImagePallete
1 parent 243402e commit f9a3178

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

Tests/test_imagepalette.py

+9
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ def test_getcolor():
5050
palette.getcolor("unknown")
5151

5252

53+
def test_getcolor_raises_on_incompatible_color():
54+
palette = ImagePalette.ImagePalette(mode="RGB")
55+
# Opaque RGBA colors should work
56+
palette.getcolor((0, 0, 0, 255))
57+
assert palette.getcolor((0, 0, 0)) == palette.getcolor((0, 0, 0, 255))
58+
with pytest.raises(ValueError):
59+
palette.getcolor((0, 0, 0, 128))
60+
61+
5362
@pytest.mark.parametrize(
5463
"index, palette",
5564
[

src/PIL/ImagePalette.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,13 @@ def getcolor(self, color, image=None):
114114
if self.rawmode:
115115
raise ValueError("palette contains raw palette data")
116116
if isinstance(color, tuple):
117-
if self.mode == "RGB":
118-
if len(color) == 4 and color[3] == 255:
117+
if self.mode == "RGB" and len(color) == 4:
118+
if color[3] == 255:
119119
color = color[:3]
120+
else:
121+
raise ValueError(
122+
"RGB ImagePalette can't handle non-opaque RGBA colors"
123+
)
120124
elif self.mode == "RGBA":
121125
if len(color) == 3:
122126
color += (255,)

0 commit comments

Comments
 (0)