Skip to content

Fill through pixels that already have the fill color when border is given - #10714

Open
farisbasha wants to merge 2 commits into
python-pillow:mainfrom
farisbasha:floodfill-border-fill-color
Open

farisbasha wants to merge 2 commits into
python-pillow:mainfrom
farisbasha:floodfill-border-fill-color

Conversation

@farisbasha

Copy link
Copy Markdown

Fixes #10712.

Changes proposed in this pull request:

  • With border, floodfill() now treats a pixel that already has the fill color as part of the region, as the docs say ("pixels with a color different from the border color"). Before, such a pixel acted as a second border, and the area behind it was not filled.
  • With border, the early return when the seed pixel already has the fill color is skipped, so the fill still spreads from it. Without border nothing changes.
  • Added tests: the two rows from the issue, and a frame with a line of the fill color inside it. They fail on main and pass with this change.

About visiting pixels twice (mentioned in the issue): no extra set is needed. On a 4-connected grid, the neighbors of a pixel in the current edge are always in the previous edge (kept in full_edge) or in the next one, so a pixel is not processed again. On the server I compared the new code with a simple BFS fill on 300 random images (0 differences), and an 800x800 image that is already all fill color takes about the same time as a plain one.

Tests/test_imagedraw.py passes (278 passed), and ruff check / ruff format --check are clean.

This change was written with help from an AI coding assistant (Claude).

@Terru03

Terru03 commented Dec 3, 2026

Copy link
Copy Markdown

I verified the traversal fix at a583f12 and found a regression when the seed is on the border and the fill and border colors are equal.

from PIL import Image, ImageDraw

im = Image.frombytes('L', (3, 1), bytes([0, 2, 0]))
ImageDraw.floodfill(im, (1, 0), 2, border=2)
print(list(im.tobytes()))

main leaves [0, 2, 0]. This PR returns [2, 2, 2]. On a separate [0, 2, 0, 1] row, floodfill(im, (-1, 0), 1, border=2) starts traversing from the left even though PixelAccess reads the last pixel. It returns [1, 2, 0, 1]. The equivalent positive seed (3, 0) returns [0, 2, 1, 1].

Tested patch. It handles alternate border color encodings while preserving scalar comparisons, exits for a border-colored seed, and normalizes the seed coordinates before traversal. The previous-frontier tracking still works. On Windows, 27 focused tests passed using the patched ImageDraw.py with the Pillow 13.0.0 native extension. I also compared 34,992 small-grid fills against a reference traversal. The source build, self-test, and drawing suite passed on Ubuntu. Linux CI.

OpenAI Codex assisted with the patch and verification.

@farisbasha

Copy link
Copy Markdown
Author

Thanks, both are real regressions from my change. Fixed in 26d885c:

  • If the seed already has the fill color and that color is also the border color, it returns early again, as on main. So [0, 2, 0] stays [0, 2, 0].
  • Negative seed coordinates are converted to the real position after the pixel is read, so (-1, 0) now behaves like (3, 0) and gives [0, 2, 1, 1].

I added tests for both cases. Tests/test_imagedraw.py passes (281) and ruff is clean. I kept the change small and left out the other color-encoding changes from your branch, to keep this PR focused on #10712.

@radarhere radarhere added the 🤖-assisted AI-assisted label Dec 3, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖-assisted AI-assisted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ImageDraw.floodfill() with border leaves part of the region unfilled when a pixel already has the fill color

3 participants