diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index 01a9b36a40f..1542dcf67af 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -1136,6 +1136,61 @@ def test_floodfill_rgb_on_rgba() -> None: assert im.getpixel(xy) == rgb + (255,) +@pytest.mark.parametrize("pixels", ([0, 0, 1, 0, 0], [1, 0, 0, 0, 0])) +def test_floodfill_border_through_fill_color(pixels: list[int]) -> None: + # Arrange + # No pixel has the border color, so the region is the whole row + im = Image.new("L", (5, 1)) + im.frombytes(bytes(pixels)) + + # Act + ImageDraw.floodfill(im, (0, 0), 1, border=2) + + # Assert + assert im.tobytes() == bytes([1] * 5) + + +def test_floodfill_border_line_of_fill_color() -> None: + # Arrange + im = Image.new("RGB", (40, 20), "white") + draw = ImageDraw.Draw(im) + draw.rectangle((2, 2, 37, 17), outline="black") + draw.line((20, 3, 20, 16), fill="red") + + # Act + ImageDraw.floodfill(im, (5, 10), (255, 0, 0), border=(0, 0, 0)) + + # Assert + assert im.getpixel((10, 10)) == (255, 0, 0) + assert im.getpixel((30, 10)) == (255, 0, 0) + assert im.getpixel((1, 1)) == (255, 255, 255) + + +def test_floodfill_border_seed_on_border() -> None: + # Arrange + im = Image.new("L", (3, 1)) + im.frombytes(bytes([0, 2, 0])) + + # Act + ImageDraw.floodfill(im, (1, 0), 2, border=2) + + # Assert + assert im.tobytes() == bytes([0, 2, 0]) + + +@pytest.mark.parametrize("xy", ((3, 0), (-1, 0))) +def test_floodfill_border_negative_seed(xy: tuple[int, int]) -> None: + # Arrange + im = Image.new("L", (4, 1)) + im.frombytes(bytes([0, 2, 0, 1])) + + # Act + ImageDraw.floodfill(im, xy, 1, border=2) + + # Assert + assert im.tobytes() == bytes([0, 2, 1, 1]) + + def create_base_image_draw( size: tuple[int, int], mode: str = DEFAULT_MODE, diff --git a/src/PIL/ImageDraw.py b/src/PIL/ImageDraw.py index 900e4a66c07..e65fe776a12 100644 --- a/src/PIL/ImageDraw.py +++ b/src/PIL/ImageDraw.py @@ -917,6 +917,11 @@ def floodfill( background = pixel[x, y] except IndexError: return # seed point outside image + # Pixel access accepts negative coordinates, counting from the far edge + if x < 0: + x += image.width + if y < 0: + y += image.height def _update_color(color: float | tuple[int, ...]) -> float | tuple[int, ...]: if image.mode == "RGBA" and isinstance(color, tuple) and len(color) == 3: @@ -924,11 +929,12 @@ def _update_color(color: float | tuple[int, ...]) -> float | tuple[int, ...]: return color value = _update_color(value) - if _color_diff(value, background) <= thresh: - return # seed point already has fill color - if border is not None: border = _update_color(border) + if _color_diff(value, background) <= thresh and ( + border is None or background == border + ): + return # seed point already has fill color pixel[x, y] = value edge = {(x, y)} # use a set to keep record of current and previous edge pixels @@ -950,7 +956,11 @@ def _update_color(color: float | tuple[int, ...]) -> float | tuple[int, ...]: if border is None: fill = _color_diff(p, background) <= thresh else: - fill = p not in (value, border) + # A pixel that already has the fill color is still + # part of the region, so the fill goes on through it. + # Pixels are not visited twice: on a 4-connected grid, + # a neighbor is always in the previous or next edge. + fill = p != border if fill: pixel[s, t] = value new_edge.add((s, t))