Skip to content

Commit 8f9643e

Browse files
authored
Limit radius to half width or height of rounded rectangle (#9561)
2 parents b902571 + de1a42e commit 8f9643e

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

Tests/test_imagedraw.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,6 +945,21 @@ def test_rounded_rectangle_zero_radius(bbox: Coords) -> None:
945945
assert_image_equal_tofile(im, "Tests/images/imagedraw_rectangle_width_fill.png")
946946

947947

948+
@pytest.mark.parametrize("w, h", ((200, 100), (100, 200)))
949+
def test_rounded_rectangle_large_radius(w: int, h: int) -> None:
950+
im = Image.new("RGB", (w, h))
951+
draw = ImageDraw.Draw(im)
952+
draw.rounded_rectangle(
953+
(0, 0, w, h), 100, "red", corners=(True, False, False, False)
954+
)
955+
956+
expected = Image.new("RGB", (w, h))
957+
draw = ImageDraw.Draw(expected)
958+
draw.rounded_rectangle((0, 0, w, h), 50, "red", corners=(True, False, False, False))
959+
960+
assert_image_equal(im, expected)
961+
962+
948963
@pytest.mark.parametrize(
949964
"xy, suffix",
950965
[

docs/reference/ImageDraw.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ Methods
382382
:param xy: Two points to define the bounding box. Sequence of either
383383
``[(x0, y0), (x1, y1)]`` or ``[x0, y0, x1, y1]``, where ``x1 >= x0`` and
384384
``y1 >= y0``. The bounding box is inclusive of both endpoints.
385-
:param radius: Radius of the corners.
385+
:param radius: Radius of the corners, limited to half of the smallest dimension of
386+
the bounding box.
386387
:param fill: Color to use for the fill.
387388
:param outline: Color to use for the outline.
388389
:param width: The line width, in pixels.

src/PIL/ImageDraw.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def rounded_rectangle(
419419
if corners is None:
420420
corners = (True, True, True, True)
421421

422-
d = radius * 2
422+
d = min(x1 - x0, y1 - y0, radius * 2)
423423

424424
x0 = round(x0)
425425
y0 = round(y0)

0 commit comments

Comments
 (0)