Limit radius to half width or height of rounded rectangle

This commit is contained in:
Andrew Murray 2026-04-13 20:46:00 +10:00
parent 3a3dab8bb0
commit de1a42e3cc
3 changed files with 18 additions and 2 deletions

View File

@ -945,6 +945,21 @@ def test_rounded_rectangle_zero_radius(bbox: Coords) -> None:
assert_image_equal_tofile(im, "Tests/images/imagedraw_rectangle_width_fill.png")
@pytest.mark.parametrize("w, h", ((200, 100), (100, 200)))
def test_rounded_rectangle_large_radius(w: int, h: int) -> None:
im = Image.new("RGB", (w, h))
draw = ImageDraw.Draw(im)
draw.rounded_rectangle(
(0, 0, w, h), 100, "red", corners=(True, False, False, False)
)
expected = Image.new("RGB", (w, h))
draw = ImageDraw.Draw(expected)
draw.rounded_rectangle((0, 0, w, h), 50, "red", corners=(True, False, False, False))
assert_image_equal(im, expected)
@pytest.mark.parametrize(
"xy, suffix",
[

View File

@ -382,7 +382,8 @@ Methods
:param xy: Two points to define the bounding box. Sequence of either
``[(x0, y0), (x1, y1)]`` or ``[x0, y0, x1, y1]``, where ``x1 >= x0`` and
``y1 >= y0``. The bounding box is inclusive of both endpoints.
:param radius: Radius of the corners.
:param radius: Radius of the corners, limited to half of the smallest dimension of
the bounding box.
:param fill: Color to use for the fill.
:param outline: Color to use for the outline.
:param width: The line width, in pixels.

View File

@ -419,7 +419,7 @@ class ImageDraw:
if corners is None:
corners = (True, True, True, True)
d = radius * 2
d = min(x1 - x0, y1 - y0, radius * 2)
x0 = round(x0)
y0 = round(y0)