From 56f30ef79228eaa57d950e1b266b58356bcfbc14 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sun, 5 Jan 2020 19:49:06 +1100 Subject: [PATCH] Allow explicit zero width to hide outline --- Tests/images/imagedraw_chord_zero_width.png | Bin 0 -> 427 bytes Tests/images/imagedraw_ellipse_zero_width.png | Bin 0 -> 339 bytes .../images/imagedraw_pieslice_zero_width.png | Bin 0 -> 403 bytes .../images/imagedraw_rectangle_zero_width.png | Bin 0 -> 212 bytes Tests/test_imagedraw.py | 48 ++++++++++++++++++ docs/reference/ImageDraw.rst | 8 +-- src/PIL/ImageDraw.py | 16 +++--- 7 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 Tests/images/imagedraw_chord_zero_width.png create mode 100644 Tests/images/imagedraw_ellipse_zero_width.png create mode 100644 Tests/images/imagedraw_pieslice_zero_width.png create mode 100644 Tests/images/imagedraw_rectangle_zero_width.png diff --git a/Tests/images/imagedraw_chord_zero_width.png b/Tests/images/imagedraw_chord_zero_width.png new file mode 100644 index 0000000000000000000000000000000000000000..c1c0058d766c76757ca24dc7503b64a66f288cdf GIT binary patch literal 427 zcmeAS@N?(olHy`uVBq!ia0vp^DImurTEP!%J(+eqMJtDqXcYE&qM{irn?P z>rbA3eeIiF)c!|7t!7!ouh~>5Ga>Br(%z{>SM2t3#U0i+ zd8yhnwfYd>-jIzV>DuqN9yhhQ`fbvYNpdSUgxBdDPFz)$TgL0Jy0@%XRCMp8StIa$mYDXGy==$0BG=E{=yW+d zXN|y@Su)z2s(JfGSMGgw+-r*D>Z|;VmLG{VjO6avs$dzmefEsq@@m>~E_~aqsePxgz5cP1-HL0ei(!Gs!0_)jbM^&Y TAML6$njk?>S3j3^P67koHc8A~jnENAQ za_ZHDZ_`STpIWu@LGS9@ZH+TGWvQ43|DDL38e0FvKD2mVu0%lCg57Jf&h1`hb=W7& zA+qt>rd7ske7CC3T+^_L=@gfS_5u_}-8&YLGRFFUtYCWkA*Q@dY&nM;*qxc=EV*v9 zG4^}1*)&K;RAelWlm7SYft{EKMcH9Mtb<9>Sk zMV&gc?aa}CC%5FTj4nDkdrc+#s|khax)UVo%1_P&M9T48SsI>W^M!b0`$?gBa`cg>%N zlh$d>Uvnnz)z(k*Tz=(-pPR4p{+v)<<4n^%tomyX2zu`m=Dyb!=pXUnn@8OHhJ|vgj9_WK64C@<$fL_jL7h JS?83{1OP8CH+292 literal 0 HcmV?d00001 diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index 4535a4838e2..863832bb72d 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -214,6 +214,18 @@ def test_chord_width_fill(self): # Assert self.assert_image_similar(im, Image.open(expected), 1) + def test_chord_zero_width(self): + # Arrange + im = Image.new("RGB", (W, H)) + draw = ImageDraw.Draw(im) + + # Act + draw.chord(BBOX1, 10, 260, fill="red", outline="yellow", width=0) + + # Assert + with Image.open("Tests/images/imagedraw_chord_zero_width.png") as expected: + self.assert_image_equal(im, expected) + def helper_ellipse(self, mode, bbox): # Arrange im = Image.new(mode, (W, H)) @@ -290,6 +302,18 @@ def test_ellipse_width_fill(self): # Assert self.assert_image_similar(im, Image.open(expected), 1) + def test_ellipse_zero_width(self): + # Arrange + im = Image.new("RGB", (W, H)) + draw = ImageDraw.Draw(im) + + # Act + draw.ellipse(BBOX1, fill="green", outline="blue", width=0) + + # Assert + with Image.open("Tests/images/imagedraw_ellipse_zero_width.png") as expected: + self.assert_image_equal(im, expected) + def helper_line(self, points): # Arrange im = Image.new("RGB", (W, H)) @@ -392,6 +416,18 @@ def test_pieslice_width_fill(self): # Assert self.assert_image_similar(im, Image.open(expected), 1) + def test_pieslice_zero_width(self): + # Arrange + im = Image.new("RGB", (W, H)) + draw = ImageDraw.Draw(im) + + # Act + draw.pieslice(BBOX1, 10, 260, fill="white", outline="blue", width=0) + + # Assert + with Image.open("Tests/images/imagedraw_pieslice_zero_width.png") as expected: + self.assert_image_equal(im, expected) + def helper_point(self, points): # Arrange im = Image.new("RGB", (W, H)) @@ -496,6 +532,18 @@ def test_rectangle_width_fill(self): # Assert self.assert_image_equal(im, Image.open(expected)) + def test_rectangle_zero_width(self): + # Arrange + im = Image.new("RGB", (W, H)) + draw = ImageDraw.Draw(im) + + # Act + draw.rectangle(BBOX1, fill="blue", outline="green", width=0) + + # Assert + with Image.open("Tests/images/imagedraw_rectangle_zero_width.png") as expected: + self.assert_image_equal(im, expected) + def test_rectangle_I16(self): # Arrange im = Image.new("I;16", (W, H)) diff --git a/docs/reference/ImageDraw.rst b/docs/reference/ImageDraw.rst index 51eaf925ec6..4a86a31f771 100644 --- a/docs/reference/ImageDraw.rst +++ b/docs/reference/ImageDraw.rst @@ -154,7 +154,7 @@ Methods To paste pixel data into an image, use the :py:meth:`~PIL.Image.Image.paste` method on the image itself. -.. py:method:: PIL.ImageDraw.ImageDraw.chord(xy, start, end, fill=None, outline=None, width=0) +.. py:method:: PIL.ImageDraw.ImageDraw.chord(xy, start, end, fill=None, outline=None, width=1) Same as :py:meth:`~PIL.ImageDraw.ImageDraw.arc`, but connects the end points with a straight line. @@ -168,7 +168,7 @@ Methods .. versionadded:: 5.3.0 -.. py:method:: PIL.ImageDraw.ImageDraw.ellipse(xy, fill=None, outline=None, width=0) +.. py:method:: PIL.ImageDraw.ImageDraw.ellipse(xy, fill=None, outline=None, width=1) Draws an ellipse inside the given bounding box. @@ -198,7 +198,7 @@ Methods .. versionadded:: 5.3.0 -.. py:method:: PIL.ImageDraw.ImageDraw.pieslice(xy, start, end, fill=None, outline=None, width=0) +.. py:method:: PIL.ImageDraw.ImageDraw.pieslice(xy, start, end, fill=None, outline=None, width=1) Same as arc, but also draws straight lines between the end points and the center of the bounding box. @@ -236,7 +236,7 @@ Methods :param outline: Color to use for the outline. :param fill: Color to use for the fill. -.. py:method:: PIL.ImageDraw.ImageDraw.rectangle(xy, fill=None, outline=None, width=0) +.. py:method:: PIL.ImageDraw.ImageDraw.rectangle(xy, fill=None, outline=None, width=1) Draws a rectangle. diff --git a/src/PIL/ImageDraw.py b/src/PIL/ImageDraw.py index c6e12150e8a..7abd459f97d 100644 --- a/src/PIL/ImageDraw.py +++ b/src/PIL/ImageDraw.py @@ -134,20 +134,20 @@ def bitmap(self, xy, bitmap, fill=None): if ink is not None: self.draw.draw_bitmap(xy, bitmap.im, ink) - def chord(self, xy, start, end, fill=None, outline=None, width=0): + def chord(self, xy, start, end, fill=None, outline=None, width=1): """Draw a chord.""" ink, fill = self._getink(outline, fill) if fill is not None: self.draw.draw_chord(xy, start, end, fill, 1) - if ink is not None and ink != fill: + if ink is not None and ink != fill and width != 0: self.draw.draw_chord(xy, start, end, ink, 0, width) - def ellipse(self, xy, fill=None, outline=None, width=0): + def ellipse(self, xy, fill=None, outline=None, width=1): """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) def line(self, xy, fill=None, width=0, joint=None): @@ -219,12 +219,12 @@ def shape(self, shape, fill=None, outline=None): if ink is not None and ink != fill: self.draw.draw_outline(shape, ink, 0) - def pieslice(self, xy, start, end, fill=None, outline=None, width=0): + def pieslice(self, xy, start, end, fill=None, outline=None, width=1): """Draw a pieslice.""" ink, fill = self._getink(outline, fill) if fill is not None: self.draw.draw_pieslice(xy, start, end, fill, 1) - if ink is not None and ink != fill: + if ink is not None and ink != fill and width != 0: self.draw.draw_pieslice(xy, start, end, ink, 0, width) def point(self, xy, fill=None): @@ -241,12 +241,12 @@ def polygon(self, xy, fill=None, outline=None): if ink is not None and ink != fill: self.draw.draw_polygon(xy, ink, 0) - def rectangle(self, xy, fill=None, outline=None, width=0): + def rectangle(self, xy, fill=None, outline=None, width=1): """Draw a rectangle.""" ink, fill = self._getink(outline, fill) if fill is not None: self.draw.draw_rectangle(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_rectangle(xy, ink, 0, width) def _multiline_check(self, text):