Skip to content

Correct error message for matrix length - #9956

Merged
radarhere merged 1 commit into
python-pillow:mainfrom
radarhere:tuple
Sep 5, 2026
Merged

radarhere merged 1 commit into
python-pillow:mainfrom
radarhere:tuple

Conversation

@radarhere

Copy link
Copy Markdown
Member

Suggested in #9726 (comment)

Before

>>> from PIL import Image
>>> im = Image.new("RGB", (1, 1))
>>> im.convert("L", matrix=(0,))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "PIL/Image.py", line 1088, in convert
    im = self.im.convert_matrix(mode, matrix)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: argument 2 must be sequence of length 12, not 1

However, matrix can actually have length 4 or 12

After

>>> from PIL import Image
>>> im = Image.new("RGB", (1, 1))
>>> im.convert("L", matrix=(0,))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "PIL/Image.py", line 1088, in convert
    im = self.im.convert_matrix(mode, matrix)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: matrix must be tuple of length 4 or 12

@akx akx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's nice!

@radarhere
radarhere merged commit ab7e054 into python-pillow:main Sep 5, 2026
57 checks passed
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.

2 participants