mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 18:06:18 +03:00
Allow converting an image to a numpy array to raise errors
This commit is contained in:
parent
ee079ae67e
commit
77a8a53a94
|
@ -4,26 +4,32 @@ from PIL import Image
|
||||||
|
|
||||||
from .helper import hopper
|
from .helper import hopper
|
||||||
|
|
||||||
|
numpy = pytest.importorskip("numpy", reason="NumPy not installed")
|
||||||
|
|
||||||
im = hopper().resize((128, 100))
|
im = hopper().resize((128, 100))
|
||||||
|
|
||||||
|
|
||||||
def test_toarray():
|
def test_toarray():
|
||||||
def test(mode):
|
def test(mode):
|
||||||
ai = im.convert(mode).__array_interface__
|
ai = numpy.array(im.convert(mode))
|
||||||
return ai["version"], ai["shape"], ai["typestr"], len(ai["data"])
|
return ai.shape, ai.dtype.str, ai.nbytes
|
||||||
|
|
||||||
# assert test("1") == (3, (100, 128), '|b1', 1600))
|
# assert test("1") == ((100, 128), '|b1', 1600))
|
||||||
assert test("L") == (3, (100, 128), "|u1", 12800)
|
assert test("L") == ((100, 128), "|u1", 12800)
|
||||||
|
|
||||||
# FIXME: wrong?
|
# FIXME: wrong?
|
||||||
assert test("I") == (3, (100, 128), Image._ENDIAN + "i4", 51200)
|
assert test("I") == ((100, 128), Image._ENDIAN + "i4", 51200)
|
||||||
# FIXME: wrong?
|
# FIXME: wrong?
|
||||||
assert test("F") == (3, (100, 128), Image._ENDIAN + "f4", 51200)
|
assert test("F") == ((100, 128), Image._ENDIAN + "f4", 51200)
|
||||||
|
|
||||||
assert test("LA") == (3, (100, 128, 2), "|u1", 25600)
|
assert test("LA") == ((100, 128, 2), "|u1", 25600)
|
||||||
assert test("RGB") == (3, (100, 128, 3), "|u1", 38400)
|
assert test("RGB") == ((100, 128, 3), "|u1", 38400)
|
||||||
assert test("RGBA") == (3, (100, 128, 4), "|u1", 51200)
|
assert test("RGBA") == ((100, 128, 4), "|u1", 51200)
|
||||||
assert test("RGBX") == (3, (100, 128, 4), "|u1", 51200)
|
assert test("RGBX") == ((100, 128, 4), "|u1", 51200)
|
||||||
|
|
||||||
|
with Image.open("Tests/images/truncated_jpeg.jpg") as im_truncated:
|
||||||
|
with pytest.raises(OSError):
|
||||||
|
numpy.array(im_truncated)
|
||||||
|
|
||||||
|
|
||||||
def test_fromarray():
|
def test_fromarray():
|
||||||
|
@ -39,10 +45,18 @@ def test_fromarray():
|
||||||
|
|
||||||
def test(mode):
|
def test(mode):
|
||||||
i = im.convert(mode)
|
i = im.convert(mode)
|
||||||
a = i.__array_interface__
|
a = numpy.array(i)
|
||||||
a["strides"] = 1 # pretend it's non-contiguous
|
|
||||||
# Make wrapper instance for image, new array interface
|
# Make wrapper instance for image, new array interface
|
||||||
wrapped = Wrapper(i, a)
|
wrapped = Wrapper(
|
||||||
|
i,
|
||||||
|
{
|
||||||
|
"shape": a.shape,
|
||||||
|
"typestr": a.dtype.str,
|
||||||
|
"version": 3,
|
||||||
|
"data": a.data,
|
||||||
|
"strides": 1, # pretend it's non-contiguous
|
||||||
|
},
|
||||||
|
)
|
||||||
out = Image.fromarray(wrapped)
|
out = Image.fromarray(wrapped)
|
||||||
return out.mode, out.size, list(i.getdata()) == list(out.getdata())
|
return out.mode, out.size, list(i.getdata()) == list(out.getdata())
|
||||||
|
|
||||||
|
|
|
@ -676,9 +676,10 @@ class Image:
|
||||||
raise ValueError("Could not save to PNG for display") from e
|
raise ValueError("Could not save to PNG for display") from e
|
||||||
return b.getvalue()
|
return b.getvalue()
|
||||||
|
|
||||||
@property
|
def __array__(self):
|
||||||
def __array_interface__(self):
|
|
||||||
# numpy array interface support
|
# numpy array interface support
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
new = {}
|
new = {}
|
||||||
shape, typestr = _conv_type_shape(self)
|
shape, typestr = _conv_type_shape(self)
|
||||||
new["shape"] = shape
|
new["shape"] = shape
|
||||||
|
@ -690,7 +691,11 @@ class Image:
|
||||||
new["data"] = self.tobytes("raw", "L")
|
new["data"] = self.tobytes("raw", "L")
|
||||||
else:
|
else:
|
||||||
new["data"] = self.tobytes()
|
new["data"] = self.tobytes()
|
||||||
return new
|
|
||||||
|
class ArrayData:
|
||||||
|
__array_interface__ = new
|
||||||
|
|
||||||
|
return np.array(ArrayData())
|
||||||
|
|
||||||
def __getstate__(self):
|
def __getstate__(self):
|
||||||
return [self.info, self.mode, self.size, self.getpalette(), self.tobytes()]
|
return [self.info, self.mode, self.size, self.getpalette(), self.tobytes()]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user