Skip to content
New issue

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

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

Already on GitHub? # to your account

ValueError while drawing Rectangle into the image #7060

Closed
NoName115 opened this issue Apr 2, 2023 · 1 comment
Closed

ValueError while drawing Rectangle into the image #7060

NoName115 opened this issue Apr 2, 2023 · 1 comment

Comments

@NoName115
Copy link

NoName115 commented Apr 2, 2023

What did you do?

I updated Pillow package from 9.4.0 to 9.5.0 and run my code for drawing rectangles into the image.

What did you expect to happen?

Code will pass without any error. Same as it worked with version 9.4.0.

What actually happened?

I received the ValueError exception:

self = <PIL.ImageDraw.ImageDraw object at 0x137035970>
xy = ((100, 100), (130, 89)), fill = -3373753, outline = None, width = 1

    def rectangle(self, xy, fill=None, outline=None, width=1):
        """Draw a rectangle."""
        ink, fill = self._getink(outline, fill)
        if fill is not None:
>           self.draw.draw_rectangle(xy, fill, 1)
E           ValueError: y1 must be greater than or equal to y0

What are your OS, Python and Pillow versions?

  • OS: macOS Ventura 13.2
  • Python: 3.9.6
  • Pillow: 9.5.0
pil_image = Image.fromarray(image_copy)
draw = ImageDraw.Draw(pil_image)

draw.rectangle(
    xy=(
        (box.x_min, box.y_min),
        (box.x_min + text_size[0], box.y_min - text_size[1]),
    ),
    fill=color,
)
@radarhere
Copy link
Member

radarhere commented Apr 2, 2023

Hi. This change was made deliberately in #6978.

https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html#PIL.ImageDraw.ImageDraw.rectangle states that the first argument is

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.

You can see a longer discussion about this in #6956, but we found that if a user had the co-ordinates in the incorrect order, this could lead to problems. This outline doesn't look correct.

from PIL import Image, ImageDraw
xy = (190, 20, 10, 180)
im = Image.new("RGB", (200, 200))
draw = ImageDraw.Draw(im)
draw.rectangle(xy, fill="red", outline="green", width=5)
im.show()

broken
But with the co-ordinates in the right order, it does.

from PIL import Image, ImageDraw
xy = (10, 20, 190, 180)
im = Image.new("RGB", (200, 200))
draw = ImageDraw.Draw(im)
draw.rectangle(xy, fill="red", outline="green", width=5)
im.show()

fixed

We also felt that it was possible for users to meant to have the co-ordinates in the order that Pillow requests them, but to have them unintentionally be the wrong way around due to a bug in their code, and that it would be helpful for users if we pointed out this bug, rather than letting it slip by.

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

No branches or pull requests

2 participants