Skip to content

Commit

Permalink
Added width argument to polygon
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Aug 29, 2021
1 parent d50052a commit 83d124e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 9 deletions.
Binary file added Tests/images/imagedraw/triangle_right_width.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions Tests/test_imagedraw.py
Expand Up @@ -932,6 +932,18 @@ def test_triangle_right():
)


@pytest.mark.parametrize(
"fill, suffix",
((BLACK, "width"), (None, "width_no_fill")),
)
def test_triangle_right_width(fill, suffix):
img, draw = create_base_image_draw((100, 100))
draw.polygon([(15, 25), (85, 25), (50, 60)], fill, WHITE, width=5)
assert_image_equal_tofile(
img, os.path.join(IMAGES_PATH, "triangle_right_" + suffix + ".png")
)


def test_line_horizontal():
img, draw = create_base_image_draw((20, 20))
draw.line((5, 5, 14, 5), BLACK, 2)
Expand Down
25 changes: 23 additions & 2 deletions src/PIL/ImageDraw.py
Expand Up @@ -235,13 +235,34 @@ def point(self, xy, fill=None):
if ink is not None:
self.draw.draw_points(xy, ink)

def polygon(self, xy, fill=None, outline=None):
def polygon(self, xy, fill=None, outline=None, width=1):
"""Draw a polygon."""
ink, fill = self._getink(outline, fill)
if fill is not None:
self.draw.draw_polygon(xy, fill, 1)
if ink is not None and ink != fill:
self.draw.draw_polygon(xy, ink, 0)
if width == 1:
self.draw.draw_polygon(xy, ink, 0, width)
else:
# To avoid expanding the polygon outwards,
# use the fill as a mask
mask = Image.new("1", self.im.size)

fill_im = mask.copy()
draw = Draw(fill_im)
draw.draw.draw_polygon(xy, 1, 1)

ink_im = mask.copy()
draw = Draw(ink_im)
width = width * 2 - 1
draw.draw.draw_polygon(xy, 1, 0, width)

mask.paste(ink_im, mask=fill_im)

im = Image.new(self.mode, self.im.size)
draw = Draw(im)
draw.draw.draw_polygon(xy, ink, 0, width)
self.im.paste(im.im, (0, 0) + im.size, mask.im)

def regular_polygon(
self, bounding_circle, n_sides, rotation=0, fill=None, outline=None
Expand Down
5 changes: 3 additions & 2 deletions src/_imaging.c
Expand Up @@ -3124,7 +3124,8 @@ _draw_polygon(ImagingDrawObject *self, PyObject *args) {
PyObject *data;
int ink;
int fill = 0;
if (!PyArg_ParseTuple(args, "Oi|i", &data, &ink, &fill)) {
int width = 0;
if (!PyArg_ParseTuple(args, "Oi|ii", &data, &ink, &fill, &width)) {
return NULL;
}

Expand Down Expand Up @@ -3153,7 +3154,7 @@ _draw_polygon(ImagingDrawObject *self, PyObject *args) {

free(xy);

if (ImagingDrawPolygon(self->image->image, n, ixy, &ink, fill, self->blend) < 0) {
if (ImagingDrawPolygon(self->image->image, n, ixy, &ink, fill, width, self->blend) < 0) {
free(ixy);
return NULL;
}
Expand Down
15 changes: 11 additions & 4 deletions src/libImaging/Draw.c
Expand Up @@ -733,7 +733,7 @@ ImagingDrawRectangle(
}

int
ImagingDrawPolygon(Imaging im, int count, int *xy, const void *ink_, int fill, int op) {
ImagingDrawPolygon(Imaging im, int count, int *xy, const void *ink_, int fill, int width, int op) {
int i, n, x0, y0, x1, y1;
DRAW *draw;
INT32 ink;
Expand Down Expand Up @@ -781,10 +781,17 @@ ImagingDrawPolygon(Imaging im, int count, int *xy, const void *ink_, int fill, i

} else {
/* Outline */
for (i = 0; i < count - 1; i++) {
draw->line(im, xy[i * 2], xy[i * 2 + 1], xy[i * 2 + 2], xy[i * 2 + 3], ink);
if (width == 1) {
for (i = 0; i < count - 1; i++) {
draw->line(im, xy[i * 2], xy[i * 2 + 1], xy[i * 2 + 2], xy[i * 2 + 3], ink);
}
draw->line(im, xy[i * 2], xy[i * 2 + 1], xy[0], xy[1], ink);
} else {
for (i = 0; i < count - 1; i++) {
ImagingDrawWideLine(im, xy[i * 2], xy[i * 2 + 1], xy[i * 2 + 2], xy[i * 2 + 3], &ink, width, op);
}
ImagingDrawWideLine(im, xy[i * 2], xy[i * 2 + 1], xy[0], xy[1], &ink, width, op);
}
draw->line(im, xy[i * 2], xy[i * 2 + 1], xy[0], xy[1], ink);
}

return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/libImaging/Imaging.h
Expand Up @@ -487,7 +487,7 @@ ImagingDrawPieslice(
extern int
ImagingDrawPoint(Imaging im, int x, int y, const void *ink, int op);
extern int
ImagingDrawPolygon(Imaging im, int points, int *xy, const void *ink, int fill, int op);
ImagingDrawPolygon(Imaging im, int points, int *xy, const void *ink, int fill, int width, int op);
extern int
ImagingDrawRectangle(
Imaging im,
Expand Down

0 comments on commit 83d124e

Please sign in to comment.