Merge pull request #5382 from radarhere/rounded_rectangle

Round down the radius in rounded_rectangle
This commit is contained in:
Hugo van Kemenade 2021-04-07 19:31:19 +03:00 committed by GitHub
commit 75c111903c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 16 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 953 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@ -722,6 +722,29 @@ def test_rounded_rectangle(xy):
assert_image_equal_tofile(im, "Tests/images/imagedraw_rounded_rectangle.png") assert_image_equal_tofile(im, "Tests/images/imagedraw_rounded_rectangle.png")
@pytest.mark.parametrize(
"xy, radius, type",
[
((10, 20, 190, 180), 30.5, "given"),
((10, 10, 181, 190), 90, "width"),
((10, 20, 190, 181), 85, "height"),
],
)
def test_rounded_rectangle_non_integer_radius(xy, radius, type):
# Arrange
im = Image.new("RGB", (200, 200))
draw = ImageDraw.Draw(im)
# Act
draw.rounded_rectangle(xy, radius, fill="red", outline="green", width=5)
# Assert
assert_image_equal_tofile(
im,
"Tests/images/imagedraw_rounded_rectangle_non_integer_radius_" + type + ".png",
)
def test_rounded_rectangle_zero_radius(): def test_rounded_rectangle_zero_radius():
# Arrange # Arrange
im = Image.new("RGB", (W, H)) im = Image.new("RGB", (W, H))

View File

@ -282,6 +282,7 @@ class ImageDraw:
# If the corners have no curve, that is a rectangle # If the corners have no curve, that is a rectangle
return self.rectangle(xy, fill, outline, width) return self.rectangle(xy, fill, outline, width)
r = d // 2
ink, fill = self._getink(outline, fill) ink, fill = self._getink(outline, fill)
def draw_corners(pieslice): def draw_corners(pieslice):
@ -315,36 +316,28 @@ class ImageDraw:
draw_corners(True) draw_corners(True)
if full_x: if full_x:
self.draw.draw_rectangle( self.draw.draw_rectangle((x0, y0 + r + 1, x1, y1 - r - 1), fill, 1)
(x0, y0 + d / 2 + 1, x1, y1 - d / 2 - 1), fill, 1
)
else: else:
self.draw.draw_rectangle( self.draw.draw_rectangle((x0 + r + 1, y0, x1 - r - 1, y1), fill, 1)
(x0 + d / 2 + 1, y0, x1 - d / 2 - 1, y1), fill, 1
)
if not full_x and not full_y: if not full_x and not full_y:
self.draw.draw_rectangle( self.draw.draw_rectangle((x0, y0 + r + 1, x0 + r, y1 - r - 1), fill, 1)
(x0, y0 + d / 2 + 1, x0 + d / 2, y1 - d / 2 - 1), fill, 1 self.draw.draw_rectangle((x1 - r, y0 + r + 1, x1, y1 - r - 1), fill, 1)
)
self.draw.draw_rectangle(
(x1 - d / 2, y0 + d / 2 + 1, x1, y1 - d / 2 - 1), fill, 1
)
if ink is not None and ink != fill and width != 0: if ink is not None and ink != fill and width != 0:
draw_corners(False) draw_corners(False)
if not full_x: if not full_x:
self.draw.draw_rectangle( self.draw.draw_rectangle(
(x0 + d / 2 + 1, y0, x1 - d / 2 - 1, y0 + width - 1), ink, 1 (x0 + r + 1, y0, x1 - r - 1, y0 + width - 1), ink, 1
) )
self.draw.draw_rectangle( self.draw.draw_rectangle(
(x0 + d / 2 + 1, y1 - width + 1, x1 - d / 2 - 1, y1), ink, 1 (x0 + r + 1, y1 - width + 1, x1 - r - 1, y1), ink, 1
) )
if not full_y: if not full_y:
self.draw.draw_rectangle( self.draw.draw_rectangle(
(x0, y0 + d / 2 + 1, x0 + width - 1, y1 - d / 2 - 1), ink, 1 (x0, y0 + r + 1, x0 + width - 1, y1 - r - 1), ink, 1
) )
self.draw.draw_rectangle( self.draw.draw_rectangle(
(x1 - width + 1, y0 + d / 2 + 1, x1, y1 - d / 2 - 1), ink, 1 (x1 - width + 1, y0 + r + 1, x1, y1 - r - 1), ink, 1
) )
def _multiline_check(self, text): def _multiline_check(self, text):