Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrected test #2

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Tests/test_image_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,14 @@ def test_reference_counting(self):

@pytest.mark.parametrize("mode", ("P", "PA"))
def test_p_putpixel_rgb_rgba(self, mode):
for color in [(255, 0, 0), (255, 0, 0, 127)]:
for color in ((255, 0, 0), (255, 0, 0, 127 if mode == "PA" else 255)):
im = Image.new(mode, (1, 1))
access = PyAccess.new(im, False)
access.putpixel((0, 0), color)

alpha = color[3] if len(color) == 4 and mode == "PA" else 255
assert im.convert("RGBA").getpixel((0, 0)) == (255, 0, 0, alpha)
if len(color) == 3:
color += (255,)
assert im.convert("RGBA").getpixel((0, 0)) == color


class TestImagePutPixelError(AccessTest):
Expand Down
11 changes: 6 additions & 5 deletions Tests/test_imagepalette.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ 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))
def test_getcolor_rgba_color_rgb_palette():
palette = ImagePalette.ImagePalette("RGB")

# Opaque RGBA colors are converted
assert palette.getcolor((0, 0, 0, 255)) == palette.getcolor((0, 0, 0))

with pytest.raises(ValueError):
palette.getcolor((0, 0, 0, 128))

Expand Down
12 changes: 6 additions & 6 deletions src/PIL/ImagePalette.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +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" and len(color) == 4:
if color[3] == 255:
if self.mode == "RGB":
if len(color) == 4:
if color[3] != 255:
raise ValueError(
"cannot add non-opaque RGBA color to RGB palette"
)
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