Skip to content

Commit

Permalink
Merge pull request #7034 from radarhere/array_interface
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Mar 25, 2023
2 parents 20293e1 + 6a93186 commit 1ff0fb4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
9 changes: 8 additions & 1 deletion Tests/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from PIL import Image

from .helper import assert_deep_equal, assert_image, hopper
from .helper import assert_deep_equal, assert_image, hopper, skip_unless_feature

numpy = pytest.importorskip("numpy", reason="NumPy not installed")

Expand Down Expand Up @@ -219,6 +219,13 @@ def test_zero_size():
assert im.size == (0, 0)


@skip_unless_feature("libtiff")
def test_load_first():
with Image.open("Tests/images/g4_orientation_5.tif") as im:
a = numpy.array(im)
assert a.shape == (88, 590)


def test_bool():
# https://github.com/python-pillow/Pillow/issues/2044
a = numpy.zeros((10, 2), dtype=bool)
Expand Down
7 changes: 2 additions & 5 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,11 +686,7 @@ def _repr_png_(self):
@property
def __array_interface__(self):
# numpy array interface support
new = {}
shape, typestr = _conv_type_shape(self)
new["shape"] = shape
new["typestr"] = typestr
new["version"] = 3
new = {"version": 3}
try:
if self.mode == "1":
# Binary images need to be extended from bits to bytes
Expand All @@ -709,6 +705,7 @@ def __array_interface__(self):
if parse_version(numpy.__version__) < parse_version("1.23"):
warnings.warn(e)
raise
new["shape"], new["typestr"] = _conv_type_shape(self)
return new

def __getstate__(self):
Expand Down

0 comments on commit 1ff0fb4

Please sign in to comment.