Restored __array__ for libraries that do not support __array_interface__

This commit is contained in:
Andrew Murray 2022-08-06 19:52:02 +10:00
parent 92b0f2c919
commit d871ef51df
2 changed files with 12 additions and 0 deletions

View File

@ -94,3 +94,10 @@ def test_fromarray_palette():
# Assert that the Python and C palettes match
assert len(out.palette.colors) == len(out.im.getpalette()) / 3
def test_array():
i = im.convert("L")
a = numpy.array(i)
assert (a == i.__array__()).all()

View File

@ -671,6 +671,11 @@ class Image:
raise ValueError("Could not save to PNG for display") from e
return b.getvalue()
def __array__(self, dtype=None):
import numpy as np
return np.array(self, dtype)
@property
def __array_interface__(self):
# numpy array interface support