Skip to content

rounded_rectangle raises an "y1 should be > y0" even when coordinates are correctly ordered #7149

@ZRunner

Description

@ZRunner

What did you do?

I recently updated one of my programs from 9.4 to 9.5 and got this new ValueError when drawing rounded rectangles on an image.

After some trials and error, I think the bug only happens when the rectangle height equals 1 + 2*radius. I'm not 200% confident about that, but that's all I got so far.

The exact lines causing the error are

Pillow/src/PIL/ImageDraw.py

Lines 378 to 383 in 2a274a4

left = [x0, y0, x0 + r, y1]
if corners[0]:
left[1] += r + 1
if corners[3]:
left[3] -= r + 1
self.draw.draw_rectangle(left, fill, 1)

What did you expect to happen?

Well, PIL should be able to correctly calculate positions and draw my rounded corners.

What actually happened?

Traceback (most recent call last):
  File "/.../test.py", line 17, in <module>
    main()
  File "/.../test.py", line 14, in main
    draw.rounded_rectangle(coordinates, radius=3, fill=(0, 0, 0))
  File "/.../env/lib/python3.9/site-packages/PIL/ImageDraw.py", line 385, in rounded_rectangle
    self.draw.draw_rectangle(left, fill, 1)
ValueError: y1 must be greater than or equal to y0

What are your OS, Python and Pillow versions?

  • OS: macOS 13.3.1
  • Python: 3.9.16
  • Pillow: 9.5.0
from PIL import Image, ImageDraw

def main():
    # get a 1021x340 image
    img = Image.new('RGB', (20, 20), color=(255, 255, 255))
    # draw a rounded rectangle on it
    draw = ImageDraw.Draw(img)
    coordinates = (
        (5, 0),
        (15, 7)
    )
    # here's the bug:
    # on ImageDraw.py line 383, y1 from "left" will be 1px less than y0
    draw.rounded_rectangle(coordinates, radius=3, fill=(0, 0, 0))

if __name__ == '__main__':
    main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions