Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not allow floodfill to extend into negative coordinates #4017

Merged
merged 1 commit into from Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added Tests/images/imagedraw_floodfill_not_negative.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions Tests/test_imagedraw.py
Expand Up @@ -559,6 +559,24 @@ def test_floodfill_thresh(self):
# Assert
self.assert_image_equal(im, Image.open("Tests/images/imagedraw_floodfill2.png"))

def test_floodfill_not_negative(self):
# floodfill() is experimental
# Test that floodfill does not extend into negative coordinates

# Arrange
im = Image.new("RGB", (W, H))
draw = ImageDraw.Draw(im)
draw.line((W / 2, 0, W / 2, H / 2), fill="green")
draw.line((0, H / 2, W / 2, H / 2), fill="green")

# Act
ImageDraw.floodfill(im, (int(W / 4), int(H / 4)), ImageColor.getrgb("red"))

# Assert
self.assert_image_equal(
im, Image.open("Tests/images/imagedraw_floodfill_not_negative.png")
)

def create_base_image_draw(
self, size, mode=DEFAULT_MODE, background1=WHITE, background2=GRAY
):
Expand Down
5 changes: 3 additions & 2 deletions src/PIL/ImageDraw.py
Expand Up @@ -437,8 +437,9 @@ def floodfill(image, xy, value, border=None, thresh=0):
new_edge = set()
for (x, y) in edge: # 4 adjacent method
for (s, t) in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)):
if (s, t) in full_edge:
continue # if already processed, skip
# If already processed, or if a coordinate is negative, skip
if (s, t) in full_edge or s < 0 or t < 0:
continue
try:
p = pixel[s, t]
except (ValueError, IndexError):
Expand Down