-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Image._check_size should allow tuple-like values #6282
Comments
Hi. Allow me to tell a story that might not seem related at first. https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html#PIL.ImageDraw.ImageDraw.line
In #3738, we had a discussion about whether to also allow a lists of lists. The conclusion was to stick with the current behaviour - #3738 (comment). In short, the idea was that tuples were more appropriate. We can have the discussion again if you like, but I imagine that the same argument from that issue would also apply here. |
Thanks very much for the pointer. I agree it is a very similar discussion. I happen to disagree with the thinking, and hence the conclusion, but if the argument is to be continued (and not just repeated), I think it should probably continue in that comment thread to keep it together. Now, I am mulling over whether I think it is worth marshalling my (ever-so-polite, logical and persuasive!) arguments, given I will be starting with the wind against me. Again, I appreciate the background info. |
Closing, as a preference has been stated for this conversation to continue in another issue. |
When you pass an image size to methods in the Image module, they are checked with the
_check_size(size)
function. This checks whether they explicitly instances oflist
ortuple
.That is an overly-restrictive check. In keeping with Python's duck-typing, the
size
just needs to be a sequence (e.g. as defined by thecollections.abc.Sequence
of two numbers that support int().For example, this means a user cannot create a custom
Size
class that supports__len__()
,__iter__()
and__getitem__()
and may be passed to theImage.new()
method.Compare ImageFilter's
_check_size()
, which doesn't specify the type, but checks that the type supports the needed operations.(I considered that the size might be being passed to C code deeper in the internals, and needed to fit in a known data-structure. I also considered that there may be concerns about performance in the typical case (where tuples are passed). In that case,
_check_size
should do the current check, and if it fails, explicitly convert the value to a tuple of integers (if possible), and only fail if not.The text was updated successfully, but these errors were encountered: