diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index 86df0fc4078..bbd37013ff9 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -991,6 +991,14 @@ def test_floodfill(bbox: Coords) -> None: assert_image_equal(im, Image.new("RGB", (1, 1), red)) +@pytest.mark.parametrize("xy", ((-1, 1), (1, -1), (-1, -1), (-3, 0), (0, -3))) +def test_floodfill_negative_seed(xy: tuple[int, int]) -> None: + im = Image.new("RGB", (3, 3)) + original = im.copy() + ImageDraw.floodfill(im, xy, (255, 0, 0)) + assert_image_equal(im, original) + + @pytest.mark.parametrize("bbox", BBOX) def test_floodfill_border(bbox: Coords) -> None: # floodfill() is experimental diff --git a/docs/releasenotes/13.1.0.rst b/docs/releasenotes/13.1.0.rst index 5edc0d28693..c50bc22fd4f 100644 --- a/docs/releasenotes/13.1.0.rst +++ b/docs/releasenotes/13.1.0.rst @@ -49,6 +49,12 @@ TODO Other changes ============= +Negative seed coordinates in ImageDraw.floodfill +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +:py:func:`~PIL.ImageDraw.floodfill` now leaves the image unchanged when either +seed coordinate is negative, instead of wrapping around to the opposite edge. + Resizing I;16B and I;16N images ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/PIL/ImageDraw.py b/src/PIL/ImageDraw.py index 19098057112..3db65a264c2 100644 --- a/src/PIL/ImageDraw.py +++ b/src/PIL/ImageDraw.py @@ -830,6 +830,8 @@ def floodfill( pixel = image.load() assert pixel is not None x, y = xy + if x < 0 or y < 0: + return try: background = pixel[x, y] if _color_diff(value, background) <= thresh: