Reverted to __array_interface__ with the release of NumPy 1.23

This commit is contained in:
Andrew Murray 2022-04-09 08:53:27 +10:00
parent 2970a5b8b2
commit e5046b1057
2 changed files with 8 additions and 12 deletions

View File

@ -1,4 +1,5 @@
import pytest
from packaging.version import parse as parse_version
from PIL import Image
@ -34,6 +35,7 @@ def test_toarray():
test_with_dtype(numpy.float64)
test_with_dtype(numpy.uint8)
if parse_version(numpy.__version__) >= parse_version("1.23"):
with Image.open("Tests/images/truncated_jpeg.jpg") as im_truncated:
with pytest.raises(OSError):
numpy.array(im_truncated)

View File

@ -671,14 +671,9 @@ class Image:
raise ValueError("Could not save to PNG for display") from e
return b.getvalue()
class _ArrayData:
def __init__(self, new):
self.__array_interface__ = new
def __array__(self, dtype=None):
@property
def __array_interface__(self):
# numpy array interface support
import numpy as np
new = {}
shape, typestr = _conv_type_shape(self)
new["shape"] = shape
@ -690,8 +685,7 @@ class Image:
new["data"] = self.tobytes("raw", "L")
else:
new["data"] = self.tobytes()
return np.array(self._ArrayData(new), dtype)
return new
def __getstate__(self):
return [self.info, self.mode, self.size, self.getpalette(), self.tobytes()]