Skip to content
Open
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
12 changes: 11 additions & 1 deletion Tests/test_imagedraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,20 @@ def test_sanity() -> None:
draw.rectangle(list(range(4)))


def test_valueerror() -> None:
def test_new_color() -> None:
with Image.open("Tests/images/chi.gif") as im:
draw = ImageDraw.Draw(im)
assert len(im.palette.colors) == 249

# Test drawing a new color onto the palette
draw.line((0, 0), fill=(0, 0, 0))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think that this is the best possible test for the fix, because it doesn’t actually draw anything. You’re only giving a single coordinate pair to ImageDraw.line.

I don’t know that it’s great that ImageDraw.line even accepts this since it’s (almost) a functional no-op, but I’m assuming that you wouldn’t want to change it now because existing code might be reliant on this behavior. (But that existing code might be broken?) Regardless, it’s not in scope for this change.

But really, as long as ImageDraw.line might accept a single coordinate pair, or even zero coordinate pairs (which it also accepts), it’d still be well within its rights to not alter the palette given that there’s nothing to draw in these cases. That it does now is maybe even a little unexpected, and it’s why I referred to the operation as “almost” a no-op above. At best, it’s probably just a side effect of the implementation, and nothing that anyone (including this test) should rely on.

I’m not sure what the original form of this test, test_valueerror, was intended to achieve, but if it’s important to get test coverage of the single coordinate pair ImageDraw.line case, I don’t think it’s prudent to replace that test with something that tests the fix for the behavior originally reported in #9308.

It would be best if the test for this palette did actual drawing that altered the image, such as by providing (at least) 2 unequal coordinate pairs to ImageDraw.draw, or by using ImageDraw.point as the test I originally proposed in #9308 did.

Even better if the test further asserted that the draw operation had an effect on the image, not just the palette, because if it’s possible for the image to not change, it’s also reasonable that the palette might not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the test to use point() and to check what is drawn afterwards.

I'm somewhat inclined to leave existing tests as they are, so I haven't adjusted the original code, but in practical terms, no, I don't think the use of line() is important here, and could easily be replaced with point().

assert len(im.palette.colors) == 250
assert im.palette.dirty

# Test drawing another new color, now that the palette is dirty
draw.point((0, 0), fill=(1, 0, 0))
assert len(im.palette.colors) == 251
assert im.convert("RGB").getpixel((0, 0)) == (1, 0, 0)


def test_mode_mismatch() -> None:
Expand Down
4 changes: 3 additions & 1 deletion src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,9 @@ def load(self) -> core.PixelAccess | None:
else:
self.im.putpalettealphas(self.info["transparency"])
self.palette.mode = "RGBA"
else:
elif self.palette.mode != mode:
# If the palette rawmode is different to the mode,
# then update the Python palette data
self.palette.palette = self.im.getpalette(
self.palette.mode, self.palette.mode
)
Expand Down
Loading