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
55 changes: 55 additions & 0 deletions Tests/test_imagedraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 14 additions & 4 deletions src/PIL/ImageDraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,18 +917,24 @@ 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:
color += (255,)
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
Expand All @@ -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))
Expand Down