Skip to content

Ignore negative seed coordinates in floodfill - #10294

Closed
Don-Yin wants to merge 1 commit into
python-pillow:mainfrom
Don-Yin:floodfill-negative-seed
Closed

Don-Yin wants to merge 1 commit into
python-pillow:mainfrom
Don-Yin:floodfill-negative-seed

Conversation

@Don-Yin

@Don-Yin Don-Yin commented Oct 23, 2026

Copy link
Copy Markdown

I tried filling a 3×3 white image from (-1, 1) and all nine pixels turned red. A seed beyond the right or bottom edge already leaves the image alone.

from PIL import Image, ImageDraw

im = Image.new("RGB", (3, 3), "white")
ImageDraw.floodfill(im, (-1, 1), (255, 0, 0))
print(im.getpixel((0, 0)))  # (255, 0, 0), despite the seed being outside

Changes proposed in this pull request:

  • I checked the seed before pixel access so negative coordinates cannot wrap around.
  • I added a regression test for negative x/y seeds and a release note.

I ran the drawing tests, python3 selftest.py, pre-commit run --all-files, and python3 -m tox -e mypy successfully. python3 -m pytest Tests/ had nine Palm/ImageGrab failures that I also reproduced on unchanged main: the local convert executable is broken and screen capture is unavailable.

The same negative seed changes all nine pixels before the fix and none after it

Signed-off-by: Don Yin <Don_Yin@outlook.com>
@radarhere

Copy link
Copy Markdown
Member

I tried filling a 3×3 white image from (-1, 1)

Can I ask - why did you try and do that?

@radarhere

Copy link
Copy Markdown
Member

What you're experiencing is not necessarily a bug.

#3406 suggested

Add a negative indexing to the PixelAccess so we can get the last pixels easily

from PIL import Image

im = Image.open("image.png")
pix = im.load()
print(pix[-1, -1])

When floodfill() goes to get the background colour, it uses the same mechanism.

background = pixel[x, y]

So to the mind of that user, this might also be considered a feature.

Pillow values backwards compatibility, so I'm inclined not to revert this behaviour.

@Don-Yin

Don-Yin commented Oct 23, 2026

Copy link
Copy Markdown
Author

Honestly, I came across it while trying edge cases rather than in a real workflow, so I don't have a strong use case for changing it. Thanks for digging up #3406; I didn't realise the wraparound came from there. I'm happy to close this. I can also send a one-line docstring note instead, saying that negative seed coordinates count from the far edge, if that would help.

@radarhere

Copy link
Copy Markdown
Member

I've created #10297.

So at https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html#PIL.ImageDraw.floodfill, the user would need to click on 'Coordinate system' to see this new note.

See what you think.

@radarhere radarhere closed this Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants