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

ImageDraw.polygon regression if polygon is a line #6275

Closed
alessiamarcolini opened this issue May 5, 2022 · 5 comments · Fixed by #6278
Closed

ImageDraw.polygon regression if polygon is a line #6275

alessiamarcolini opened this issue May 5, 2022 · 5 comments · Fixed by #6278

Comments

@alessiamarcolini
Copy link

What did you do?

The behavior of ImageDraw.polygon changed between versions 8.0.4 and 9.0.0 if the polygon is a line.
Could it be related to #5694?

import PIL
import PIL.ImageDraw
import numpy as np

line_vertices = [(1, 0), (1, 0), (2, 0), (2, 0)]
img = PIL.Image.new("L", (6, 5), 0)
PIL.ImageDraw.Draw(img).polygon(line_vertices, outline=1, fill=1)
result = np.array(img).astype(bool)

What did you expect to happen?

This was the result with Pillow 8.0.4

array([[False, False, False, False, False, False],
       [False, False, False, False, False, False],
       [False, False, False, False, False, False],
       [False, False, False, False, False, False],
       [False, False, False, False, False, False]])

What actually happened?

This is the result with Pillow >= 9.0.0

array([[False,  True,  True, False, False, False],
       [False, False, False, False, False, False],
       [False, False, False, False, False, False],
       [False, False, False, False, False, False],
       [False, False, False, False, False, False]])

What are your OS, Python and Pillow versions?

  • OS: macOS 12.13.1
  • Python: 3.9.7
  • Pillow: 8.0.4 and >= 9.0.0
@radarhere
Copy link
Member

Testing, the change is because of #5835.

Because that PR was undoing part of #4333, I checked, and yes - the Pillow >= 9 behaviour is also how Pillow behaved <= 7. So I don't think this can be considered a clear-cut regression.

Seeing that aside for a moment, could you give an explanation why you think the current behaviour is not correct? Yes, you're drawing a polygon of zero height, but you're also outlining it, so it doesn't seem unreasonable to me that something is drawn.

@alessiamarcolini
Copy link
Author

alessiamarcolini commented May 5, 2022

Thank you @radarhere for the quick reply. I was reporting it not necessarily saying that the new behavior is wrong, just I was not sure which one was the "correct" one. Also, I was not sure if you were aware of this change :)

I spotted this because this example was an edge case for a test in histolab, where we were mostly using it to document what would happen if the polygon was a line. So I'm fine changing the expectation on our end, I just wanted to make sure this was the intended result

@radarhere
Copy link
Member

#4333 wasn't intended to change which pixels were filled in, so yes, I'd say that the <=7 behaviour of your code is correct. Meaning that the behaviour >= 9 is also correct.

@radarhere
Copy link
Member

So, #4333 unintentionally changed this behaviour. #5835 switched your code back to the original approach, but left the new approach in place when translucency is involved.

I've created PR #6278. It doesn't change the output of your code, but it does fix 1px high polygons when translucency is involved - meaning that even if #5835 hadn't switched back to Pillow's behaviour <= 7, it would now.

@alessiamarcolini
Copy link
Author

Thank you @radarhere for the investigation and for confirming that this is the correct behavior!
I will change the expectation of the test on my end 💯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants