diff --git a/Tests/images/imagedraw_rounded_rectangle_non_integer_radius_given.png b/Tests/images/imagedraw_rounded_rectangle_non_integer_radius_given.png new file mode 100644 index 000000000..59e55b2a1 Binary files /dev/null and b/Tests/images/imagedraw_rounded_rectangle_non_integer_radius_given.png differ diff --git a/Tests/images/imagedraw_rounded_rectangle_non_integer_radius_height.png b/Tests/images/imagedraw_rounded_rectangle_non_integer_radius_height.png new file mode 100644 index 000000000..c4e54896b Binary files /dev/null and b/Tests/images/imagedraw_rounded_rectangle_non_integer_radius_height.png differ diff --git a/Tests/images/imagedraw_rounded_rectangle_non_integer_radius_width.png b/Tests/images/imagedraw_rounded_rectangle_non_integer_radius_width.png new file mode 100644 index 000000000..6b0f11fa6 Binary files /dev/null and b/Tests/images/imagedraw_rounded_rectangle_non_integer_radius_width.png differ diff --git a/Tests/test_imagedraw.py b/Tests/test_imagedraw.py index 06c5b2503..7f31e1aaf 100644 --- a/Tests/test_imagedraw.py +++ b/Tests/test_imagedraw.py @@ -722,6 +722,29 @@ def test_rounded_rectangle(xy): 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(): # Arrange im = Image.new("RGB", (W, H)) diff --git a/src/PIL/ImageDraw.py b/src/PIL/ImageDraw.py index 8988e4233..0573eadac 100644 --- a/src/PIL/ImageDraw.py +++ b/src/PIL/ImageDraw.py @@ -282,6 +282,7 @@ class ImageDraw: # If the corners have no curve, that is a rectangle return self.rectangle(xy, fill, outline, width) + r = d // 2 ink, fill = self._getink(outline, fill) def draw_corners(pieslice): @@ -315,36 +316,28 @@ class ImageDraw: draw_corners(True) if full_x: - self.draw.draw_rectangle( - (x0, y0 + d / 2 + 1, x1, y1 - d / 2 - 1), fill, 1 - ) + self.draw.draw_rectangle((x0, y0 + r + 1, x1, y1 - r - 1), fill, 1) else: - self.draw.draw_rectangle( - (x0 + d / 2 + 1, y0, x1 - d / 2 - 1, y1), fill, 1 - ) + self.draw.draw_rectangle((x0 + r + 1, y0, x1 - r - 1, y1), fill, 1) if not full_x and not full_y: - self.draw.draw_rectangle( - (x0, y0 + d / 2 + 1, x0 + d / 2, y1 - d / 2 - 1), fill, 1 - ) - self.draw.draw_rectangle( - (x1 - d / 2, y0 + d / 2 + 1, x1, y1 - d / 2 - 1), fill, 1 - ) + self.draw.draw_rectangle((x0, y0 + r + 1, x0 + r, y1 - r - 1), fill, 1) + self.draw.draw_rectangle((x1 - r, y0 + r + 1, x1, y1 - r - 1), fill, 1) if ink is not None and ink != fill and width != 0: draw_corners(False) if not full_x: 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( - (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: 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( - (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):