Skip to content

Commit

Permalink
Merge pull request #8326 from radarhere/numpy
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Aug 24, 2024
2 parents b6f90c4 + e382ebe commit f15034c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Tests/test_image_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_with_dtype(dtype: npt.DTypeLike) -> None:
with pytest.raises(OSError):
numpy.array(im_truncated)
else:
with pytest.warns(UserWarning):
with pytest.warns(DeprecationWarning):
numpy.array(im_truncated)


Expand Down
24 changes: 6 additions & 18 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,24 +724,12 @@ def _repr_jpeg_(self) -> bytes | None:
def __array_interface__(self) -> dict[str, str | bytes | int | tuple[int, ...]]:
# numpy array interface support
new: dict[str, str | bytes | int | tuple[int, ...]] = {"version": 3}
try:
if self.mode == "1":
# Binary images need to be extended from bits to bytes
# See: https://github.com/python-pillow/Pillow/issues/350
new["data"] = self.tobytes("raw", "L")
else:
new["data"] = self.tobytes()
except Exception as e:
if not isinstance(e, (MemoryError, RecursionError)):
try:
import numpy
from packaging.version import parse as parse_version
except ImportError:
pass
else:
if parse_version(numpy.__version__) < parse_version("1.23"):
warnings.warn(str(e))
raise
if self.mode == "1":
# Binary images need to be extended from bits to bytes
# See: https://github.com/python-pillow/Pillow/issues/350
new["data"] = self.tobytes("raw", "L")
else:
new["data"] = self.tobytes()
new["shape"], new["typestr"] = _conv_type_shape(self)
return new

Expand Down

0 comments on commit f15034c

Please sign in to comment.