Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed joined corners for ImageDraw rounded_rectangle() odd dimensions #7151

Merged
merged 1 commit into from
May 12, 2023

Conversation

radarhere
Copy link
Member

Resolves #7149

The issue has found that certain coordinates passed to rounded_rectangle() trigger ValueError: y1 must be greater than or equal to y0 at

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)

These lines are trying to draw a rectangle in between the rounded corners. In

coordinates = (
    (5, 0),
    (15, 7)
)
draw.rounded_rectangle(coordinates, radius=3, fill=(0, 0, 0))

the user isn't specifying any corners, so by default they are all on. corners[0] is lowering the top of this middle rectangle from 0 to 4, and corners[3] is raising the bottom from 7 to 3... meaning the top is now lower than the bottom, resulting in the error. In more human terms, the code has concluded that there isn't any middle rectangle that needs to be drawn, because the two corners join. This is referred to earlier in ImageDraw as full_y.

Pillow/src/PIL/ImageDraw.py

Lines 318 to 324 in 2a274a4

if full_x:
# The two left and two right corners are joined
d = x1 - x0
full_y = d >= y1 - y0
if full_y:
# The two top and two bottom corners are joined
d = y1 - y0

So this PR updates the detection of full_x and full_y to cover this scenario that has resulted from an odd height.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rounded_rectangle raises an "y1 should be > y0" even when coordinates are correctly ordered
2 participants