Skip to content

Incorrect border color in corners when drawing rectangles with border color with opacity < 1 #5182

@macjohnny

Description

@macjohnny

What did you do?

Draw a rectangle with a color with opacity < 1 for border (e.g. outline='#0000ffaa')

What did you expect to happen?

The border color should be homogenous, and not altered by the fill color, if set.

What actually happened?

image
The border color is different in the corners, probably due to overlapping line segments.
Moreover, when a fill color is set, the border color is changed due to the overlap of the border and fill rectangle.

What are your OS, Python and Pillow versions?

  • OS: Windows 10
  • Python: 3.8
  • Pillow: 8.0.1

Example

Code

import PIL.Image
import PIL.ImageDraw

image = PIL.Image.new(mode='RGB', size=(500, 500), color='#ffffff')
draw = PIL.ImageDraw.ImageDraw(image, mode='RGBA')
draw.rectangle(xy=((100,100), (200,200)),
               fill='#ffff00',
               outline='#0000ffaa',
               width=10)
draw.rectangle(xy=((205,100), (305,200)),
               outline='#0000ffaa',
               width=10)

image.save('image.png')

Result

image

Related Sources

Pillow/src/PIL/ImageDraw.py

Lines 255 to 258 in d374015

if fill is not None:
self.draw.draw_rectangle(xy, fill, 1)
if ink is not None and ink != fill and width != 0:
self.draw.draw_rectangle(xy, ink, 0, width)

Pillow/src/_imaging.c

Lines 3171 to 3180 in d374015

n = ImagingDrawRectangle(
self->image->image,
(int)xy[0],
(int)xy[1],
(int)xy[2],
(int)xy[3],
&ink,
fill,
width,
self->blend);

for (i = 0; i < width; i++) {
draw->hline(im, x0, y0 + i, x1, ink);
draw->hline(im, x0, y1 - i, x1, ink);
draw->line(im, x1 - i, y0, x1 - i, y1, ink);
draw->line(im, x0 + i, y1, x0 + i, y0, ink);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions