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

Odd behaviour of ImageDraw.Draw.polygon #4674

Closed
jmsteitz opened this issue Jun 6, 2020 · 3 comments · Fixed by #4675
Closed

Odd behaviour of ImageDraw.Draw.polygon #4674

jmsteitz opened this issue Jun 6, 2020 · 3 comments · Fixed by #4675
Labels
Bug Any unexpected behavior, until confirmed feature.

Comments

@jmsteitz
Copy link

jmsteitz commented Jun 6, 2020

What did you do?

I drew two polygons, each specified by only two vertices that are both on a vertical or horizontal line.

What did you expect to happen?

Expected to get a one pixel wide vertical and a one pixel wide horizontal line.

What actually happened?

The vertical line shows but not the horizontal one.

What are your OS, Python and Pillow versions?

  • OS: Ubuntu 18.04.4
  • Python: 3.7.6
  • Pillow: 7.1.1
import numpy as np
from PIL import Image, ImageDraw
img = Image.new("L", size=(2,2), color=0)
ImageDraw.Draw(img).polygon([(0,0),(0,1)], outline=1, fill=1)
np.array(img)

gives

array([[1, 0],
       [1, 0]], dtype=uint8)

but

import numpy as np
from PIL import Image, ImageDraw
img = Image.new("L", size=(2,2), color=0)
ImageDraw.Draw(img).polygon([(0,0),(1,0)], outline=1, fill=1)
np.array(img)

results in

array([[0, 0],
       [0, 0]], dtype=uint8)
@radarhere
Copy link
Member

I understand that you might be trying to just raise a general inconsistency with polygon, but, on a practical level, if you change polygon to line, and remove the outline argument, the following works.

import numpy as np
from PIL import Image, ImageDraw
img = Image.new("L", size=(2,2), color=0)
ImageDraw.Draw(img).line([(0,0),(1,0)], fill=1)
print(np.array(img))
[[1 1]
 [0 0]]

@radarhere
Copy link
Member

I've created PR #4675 to fix this.

@radarhere radarhere added the Bug Any unexpected behavior, until confirmed feature. label Jun 7, 2020
@jmsteitz
Copy link
Author

jmsteitz commented Jun 7, 2020

Thanks for the PR and the suggested workaround! We encountered the problem while generating mask images with arbitrary polygons that sometimes happen to be degenerated, i.e. falling onto a horizontal line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Any unexpected behavior, until confirmed feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants