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

Unexpected, additional outline in ImageDraw.ellipse, even for outline=None #4277

Closed
HansHirse opened this issue Dec 18, 2019 · 3 comments
Closed
Labels
Bug Any unexpected behavior, until confirmed feature.

Comments

@HansHirse
Copy link

What did you do?

Drawing a polygon using ImageDraw.polygon and an ellipse using ImageDraw.ellipse with no outline specified, with an opaque outline, and with a semi-transparent outline.

What did you expect to happen?

ImageDraw.polygon and ImageDraw.ellipse should behave the same for the mentioned cases.

What actually happened?

The output created by the code below:

Figure_1

Looking at the polygon, if no outline is specified (top left image), we see that the black line is visible, which is one of the polygon's borders. Specifying an opaque outline (top center image), the black line's no longer visible. Setting a semi-transparent outline (top right image) reveals, that the outline is identical to the polygon's border.

Now, the same for the ellipse: If no outline is set (top left), an outline is shown nevertheless, most likely the same color as used for the fill parameter, but without incorporating an alpha value. Setting an opaque outline (top center) "overwrites" the unexpected existent outline, but when setting a semi-transparent outline, we see that the unexpected outline is still there.

This effect becomes even more obvious, when setting width > 1 in ellipse, see the bottom row. The unexpected outline still seems to have width = 1, whereas the explicitly set outline has width = 5.

What are your OS, Python and Pillow versions?

  • OS: Windows 10 Prof.
  • Python: 3.7.1
  • Pillow: 6.2.1
from matplotlib import pyplot as plt
from PIL import Image, ImageDraw


def example(outline_alpha=None, width=None):
    if outline_alpha is None:
        outline = None
    else:
        outline = (255, 255, 0, outline_alpha)
    if width is None:
        width = 0
    img = Image.new('RGB', (100, 100), (255, 255, 255))
    drw = ImageDraw.Draw(img, 'RGBA')
    drw.line([(0, 40), (100, 40)], (0, 0, 0, 255))
    drw.line([(50, 100), (100, 0)], (0, 0, 0, 255))
    drw.polygon([(50, 100), (100, 0), (0, 0)], (0, 255, 0, 128), outline)
    drw.ellipse([(40, 40), (90, 90)], (0, 0, 255, 128), outline, width)
    return img


plt.figure(1, figsize=(15, 10))
plt.subplot(2, 3, 1), plt.imshow(example()), plt.title('No outlines specified, width = 0')
plt.subplot(2, 3, 2), plt.imshow(example(255)), plt.title('Opaque outlines specified, width = 0')
plt.subplot(2, 3, 3), plt.imshow(example(128)), plt.title('Semi-transparent outlines specified, width = 0')
plt.subplot(2, 3, 4), plt.imshow(example(None, 5)), plt.title('No outlines specified, width = 5')
plt.subplot(2, 3, 5), plt.imshow(example(255, 5)), plt.title('Opaque outlines specified, width = 5')
plt.subplot(2, 3, 6), plt.imshow(example(20, 5)), plt.title('Semi-transparent outlines specified, width = 5')
plt.tight_layout()
plt.show()
@radarhere
Copy link
Member

radarhere commented Dec 18, 2019

Making 'opaque outlines' and 'semi-transparent outlines' behave the same as 'no outlines' specified is easy enough -

    def ellipse(self, xy, fill=None, outline=None, width=0):
        """Draw an ellipse."""
        ink, fill = self._getink(outline, fill)
        if fill is not None:
            self.draw.draw_ellipse(xy, fill, 1)
-       if ink is not None and ink != fill:
+       if ink is not None and ink != fill and width != 0:
            self.draw.draw_ellipse(xy, ink, 0, width)

However, 'no outlines' may be more difficult, as it is sort of a known problem -

/* This causes the pixels of horizontal edges to be drawn twice :(
* but without it there are inconsistencies in ellipses */

If you're interested in a workaround, you could draw the ellipse without an alpha value, and then paste it with an alpha value.

@radarhere radarhere added the Bug Any unexpected behavior, until confirmed feature. label Dec 18, 2019
@radarhere
Copy link
Member

radarhere commented Jan 6, 2020

I have created PR #4333 and #4334. #4333 will remove the darker outline using the fill colour, and #4334 will stop the application of the specified outline colour for a zero width.

Screen Shot 2020-01-06 at 9 04 16 pm

Could I clarify - you have no problem with the behaviour of polygon, yes? Just ellipse? If both are merged to create the attached image, then this issue is resolved?

@HansHirse
Copy link
Author

@radarhere Correct, polygon works as I would've expected. And, yes, issue's resolved then. Thank you very much for your effort! (And, sorry for not responding back in December, these holidays...)

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

No branches or pull requests

2 participants