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

Error raising on unsorted dimensions for draw_rect/draw_rounded_rect #7217

Closed
Intenzi opened this issue Jun 17, 2023 · 4 comments
Closed

Error raising on unsorted dimensions for draw_rect/draw_rounded_rect #7217

Intenzi opened this issue Jun 17, 2023 · 4 comments

Comments

@Intenzi
Copy link

Intenzi commented Jun 17, 2023

With the change in #6978
There is a hidden error now raised due to radius argument for draw_rounded_rectangle

This is in reference to x0, x1 dimensions raising the ValueError: x1 must be greater than or equal to x0

Wherein, the passed dimensions are sorted but when sent to draw_rounded_rectangle, it utilises underlying code of:-
self.draw.draw_rectangle((x0 + r + 1, y0, x1 - r - 1, y1), fill, 1)

Dynamic code such as draw_rounded_rectangle((2, 2), (2 + variable, 10), radius=3)
[variable is >=0]
Thus implicitly fail in certain cases where the variable's value is 4 or less, which explicitly is greater than x0.
The documentation only states for x1 >= x0 and y1 >= y0, which while satisfied will raise the error in the above case.

Question:
Is there an already known solution towards it, for example utilizing x0 + variable + radius + 1 when passing x1's value to the function? Which does not seem very ideal and may cause certain changes to the image generation that I am unaware of.
Can this be documented with a possible solution in such a case?

@radarhere
Copy link
Member

So you are saying that

from PIL import Image, ImageDraw
im = Image.new("RGB", (1, 1))
draw = ImageDraw.Draw(im)
draw.rounded_rectangle(((2, 2), (5, 10)), radius=3)

fails with an error? When I try it with Pillow 9.5.0, it doesn't.

If you try the example I just posted, does it fail for you? If it doesn't, and your code does, could you provide the exact code that you're using?

@Intenzi
Copy link
Author

Intenzi commented Jun 17, 2023

Sorry for having you confused
I'm too short on coffee to reliably tell how this is occurring, but here are a few cases where it will lead to an error:-

from PIL import Image, ImageDraw
im = Image.new("RGB", (1, 1))
draw = ImageDraw.Draw(im)
draw.rounded_rectangle(((2, 2), (9, 10)), radius=3)

This leads to an error at line 382, in file -> ImageDraw.py , in rounded_rectangle
self.draw.draw_rectangle(top, ink, 1)
ValueError: x1 must be greater than or equal to x0

The error I have reported, does not get reproduced when utilizing the value as 5, just like you have said.
In my code, I am utilizing tuple shape, radius and the fill parameter

When reproduced with the fill parameter, I do get errors, but not for the reasons I assumed they would be for..
as it is not occurring with values like 3, 4, 5 but only certain values.
Here is the reproduction code, I apologize for being incomplete about my previous report.

from PIL import Image, ImageDraw
im = Image.new("RGB", (1, 1))
draw = ImageDraw.Draw(im)
draw.rounded_rectangle(((2, 2), (9, 10)), radius=3, fill=(0, 151, 62))

File ImageDraw.py, line 383, in rounded_rectangle
    self.draw.draw_rectangle((x0 + r + 1, y0, x1 - r - 1, y1), fill, 1)
ValueError: x1 must be greater than or equal to x0

With the same value 9 and fill parameter, this time the error is different and is the issue I am facing.

@radarhere
Copy link
Member

Thanks. When I test your new code, I find that it has already been fixed by #7151.

The fix will be part of Pillow 10, due to be released on July 1.

@Intenzi
Copy link
Author

Intenzi commented Jun 17, 2023

Thankyou! That should resolve it

@Intenzi Intenzi closed this as completed Jun 17, 2023
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

No branches or pull requests

2 participants