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

Clarify ImageDraw2 error message when size is missing #8165

Merged
merged 1 commit into from
Jun 24, 2024

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented Jun 24, 2024

When creating an ImageDraw2.Draw instance, image can be either an image or a mode string.

def __init__(self, image, size=None, color=None):
if not hasattr(image, "im"):
image = Image.new(image, size, color)
self.draw = ImageDraw.Draw(image)

If you provide a mode string without a size, you get an error saying that size must be a list or a tuple.

>>> from PIL import ImageDraw2
>>> ImageDraw2.Draw("RGB", size=None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "PIL/ImageDraw2.py", line 61, in __init__
    image = Image.new(image, size, color)
  File "PIL/Image.py", line 3061, in new
    _check_size(size)
  File "PIL/Image.py", line 3029, in _check_size
    raise ValueError(msg)
ValueError: Size must be a list or tuple

Except, if you provided an image as the first argument, then size can be None without a problem.

>>> from PIL import Image
>>> im = Image.new("RGB", (1, 1))
>>> ImageDraw2.Draw(im, size=None)
<PIL.ImageDraw2.Draw object at 0x10084cdf0>

This is conceivably confusing, so this PR clarifies the situation, by changing the error to

If image argument is mode string, size must be a list or tuple

@hugovk hugovk merged commit ad4c23b into python-pillow:main Jun 24, 2024
55 of 56 checks passed
@radarhere radarhere deleted the imagedraw2_error branch June 24, 2024 10:07
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.

None yet

2 participants