Add width parameter to rectangle

This commit is contained in:
Hugo 2018-04-12 18:15:27 +03:00
parent ae9f62040d
commit 9dedbff713
8 changed files with 46 additions and 14 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 228 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 239 B

View File

@ -343,6 +343,30 @@ class TestImageDraw(PillowTestCase):
# Assert # Assert
self.assert_image_similar(im, Image.open(expected), 1) self.assert_image_similar(im, Image.open(expected), 1)
def test_rectangle_width(self):
# Arrange
im = Image.new("RGB", (W, H))
draw = ImageDraw.Draw(im)
expected = "Tests/images/imagedraw_rectangle_width.png"
# Act
draw.rectangle(BBOX1, outline="green", width=5)
# Assert
self.assert_image_equal(im, Image.open(expected))
def test_rectangle_width_fill(self):
# Arrange
im = Image.new("RGB", (W, H))
draw = ImageDraw.Draw(im)
expected = "Tests/images/imagedraw_rectangle_width_fill.png"
# Act
draw.rectangle(BBOX1, fill="blue", outline="green", width=5)
# Assert
self.assert_image_equal(im, Image.open(expected))
def test_floodfill(self): def test_floodfill(self):
# Arrange # Arrange
im = Image.new("RGB", (W, H)) im = Image.new("RGB", (W, H))

View File

@ -220,7 +220,7 @@ Methods
:param outline: Color to use for the outline. :param outline: Color to use for the outline.
:param fill: Color to use for the fill. :param fill: Color to use for the fill.
.. py:method:: PIL.ImageDraw.ImageDraw.rectangle(xy, fill=None, outline=None) .. py:method:: PIL.ImageDraw.ImageDraw.rectangle(xy, fill=None, outline=None, width=0)
Draws a rectangle. Draws a rectangle.
@ -229,6 +229,9 @@ Methods
is just outside the drawn rectangle. is just outside the drawn rectangle.
:param outline: Color to use for the outline. :param outline: Color to use for the outline.
:param fill: Color to use for the fill. :param fill: Color to use for the fill.
:param width: The line width, in pixels.
.. versionadded:: 5.2.0
.. py:method:: PIL.ImageDraw.ImageDraw.shape(shape, fill=None, outline=None) .. py:method:: PIL.ImageDraw.ImageDraw.shape(shape, fill=None, outline=None)

View File

@ -186,13 +186,13 @@ class ImageDraw(object):
if ink is not None: if ink is not None:
self.draw.draw_polygon(xy, ink, 0) self.draw.draw_polygon(xy, ink, 0)
def rectangle(self, xy, fill=None, outline=None): def rectangle(self, xy, fill=None, outline=None, width=0):
"""Draw a rectangle.""" """Draw a rectangle."""
ink, fill = self._getink(outline, fill) ink, fill = self._getink(outline, fill)
if fill is not None: if fill is not None:
self.draw.draw_rectangle(xy, fill, 1) self.draw.draw_rectangle(xy, fill, 1)
if ink is not None: if ink is not None:
self.draw.draw_rectangle(xy, ink, 0) self.draw.draw_rectangle(xy, ink, 0, width)
def _multiline_check(self, text): def _multiline_check(self, text):
"""Draw text.""" """Draw text."""

View File

@ -2931,7 +2931,8 @@ _draw_rectangle(ImagingDrawObject* self, PyObject* args)
PyObject* data; PyObject* data;
int ink; int ink;
int fill = 0; 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; return NULL;
n = PyPath_Flatten(data, &xy); n = PyPath_Flatten(data, &xy);
@ -2945,7 +2946,7 @@ _draw_rectangle(ImagingDrawObject* self, PyObject* args)
n = ImagingDrawRectangle(self->image->image, n = ImagingDrawRectangle(self->image->image,
(int) xy[0], (int) xy[1], (int) xy[0], (int) xy[1],
(int) xy[2], (int) xy[3], (int) xy[2], (int) xy[3],
&ink, fill, self->blend &ink, fill, width, self->blend
); );
free(xy); free(xy);

View File

@ -634,8 +634,9 @@ ImagingDrawWideLine(Imaging im, int x0, int y0, int x1, int y1,
int int
ImagingDrawRectangle(Imaging im, int x0, int y0, int x1, int y1, ImagingDrawRectangle(Imaging im, int x0, int y0, int x1, int y1,
const void* ink_, int fill, int op) const void* ink_, int fill, int width, int op)
{ {
int i;
int y; int y;
int tmp; int tmp;
DRAW* draw; DRAW* draw;
@ -662,13 +663,16 @@ ImagingDrawRectangle(Imaging im, int x0, int y0, int x1, int y1,
draw->hline(im, x0, y, x1, ink); draw->hline(im, x0, y, x1, ink);
} else { } else {
/* outline */ /* outline */
draw->line(im, x0, y0, x1, y0, ink); if (width == 0) {
draw->line(im, x1, y0, x1, y1, ink); width = 1;
draw->line(im, x1, y1, x0, y1, ink); }
draw->line(im, x0, y1, x0, y0, ink); 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);
}
} }
return 0; return 0;

View File

@ -368,7 +368,7 @@ extern int ImagingDrawPoint(Imaging im, int x, int y, const void* ink, int op);
extern int ImagingDrawPolygon(Imaging im, int points, int *xy, extern int ImagingDrawPolygon(Imaging im, int points, int *xy,
const void* ink, int fill, int op); const void* ink, int fill, int op);
extern int ImagingDrawRectangle(Imaging im, int x0, int y0, int x1, int y1, extern int ImagingDrawRectangle(Imaging im, int x0, int y0, int x1, int y1,
const void* ink, int fill, int op); const void* ink, int fill, int width, int op);
/* Level 2 graphics (WORK IN PROGRESS) */ /* Level 2 graphics (WORK IN PROGRESS) */
extern ImagingOutline ImagingOutlineNew(void); extern ImagingOutline ImagingOutlineNew(void);