Closed musicinmybrain closed 2 days ago
The casting that allowed this to work was originally added in https://github.com/python-pillow/Pillow/pull/945 for the sake of a SciPy regression.
We stopped testing for it in https://github.com/python-pillow/Pillow/pull/3087, but left the casting.
The casting was then removed in https://github.com/python-pillow/Pillow/pull/8046
Is there any particular reason that it is helpful for resize()
to accept a NumPy array? I have to imagine that most of Pillow doesn't work well with NumPy arrays as sizes.
We're currently working on improving type hints. If you're aware of a generic type hint that supports tuples and NumPy arrays, let me know.
Is there any particular reason that it is helpful for
resize()
to accept a NumPy array? I have to imagine that most of Pillow doesn't work well with NumPy arrays as sizes.
In the abstract, I would say no. In practice, it’s a case of Hyrum’s Law: since the interface has historically accepted a NumPy array, some software depends on this behavior and breaks when it stops working. Suggesting a patch for trimesh
was fairly straightforward, but there might be cases where it’s not so easy.
We're currently working on improving type hints. If you're aware of a generic type hint that supports tuples and NumPy arrays, let me know.
There’s collections.abc.Sequence
, but a Sequence[int]
may have arbitrary length. For that reason, tuple[int, int]
is probably a better choice. Even if the annotation allowed non-tuple sequences, it would be necessary to ensure the implementation actually supported arbitrary sequence types.
Note that passing a list, e.g. newsize = [3, 7]; x.resize(newsize)
, does still work, but code wishing to respect the type annotations will need to convert to a tuple, x.resize(tuple(newsize))
. That’s probably reasonable.
What did you do?
I encountered some new test failures in https://github.com/mikedh/trimesh/issues/2247 because passing a
numpy.ndarray
as the size toImage.resize()
works in 10.3.0, but not in 10.4.0.The question is whether this is a regression in Pillow, or whether passing a
numpy.ndarray
toImage.resize()
was always “wrong,” since https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Image.resize calls for a “tuple,” and it only happened to work.What did you expect to happen?
With
numpy==2.0.0
,pillow==10.3.0
:What actually happened?
With
numpy==2.0.0
,pillow==10.4.0
:What are your OS, Python and Pillow versions?
While I originally encountered this building system RPM packages, this report is based on the above reproducer in a clean virtualenv using packages installed from PyPI.