Skip to content
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

Reverted to __array_interface__ with the release of NumPy 1.23 #6394

Merged
merged 1 commit into from Jun 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions Tests/test_image_array.py
@@ -1,4 +1,5 @@
import pytest
from packaging.version import parse as parse_version

from PIL import Image

Expand Down Expand Up @@ -34,9 +35,10 @@ def test_with_dtype(dtype):
test_with_dtype(numpy.float64)
test_with_dtype(numpy.uint8)

with Image.open("Tests/images/truncated_jpeg.jpg") as im_truncated:
with pytest.raises(OSError):
numpy.array(im_truncated)
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)


def test_fromarray():
Expand Down
12 changes: 3 additions & 9 deletions src/PIL/Image.py
Expand Up @@ -671,14 +671,9 @@ def _repr_png_(self):
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
Expand All @@ -690,8 +685,7 @@ def __array__(self, dtype=None):
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()]
Expand Down