From 47691906df10d0a7bfb485e97ba66475247189fd Mon Sep 17 00:00:00 2001 From: pwohlhart Date: Tue, 29 Oct 2019 10:12:03 -0700 Subject: [PATCH 1/2] Better error messaging in PIL.image.fromarray --- src/PIL/Image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 474ca1e88..0c783a428 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -2633,7 +2633,7 @@ def fromarray(obj, mode=None): typekey = (1, 1) + shape[2:], arr["typestr"] mode, rawmode = _fromarray_typemap[typekey] except KeyError: - raise TypeError("Cannot handle this data type") + raise TypeError("Cannot handle this data type: %s, %s" % typekey) else: rawmode = mode if mode in ["1", "L", "I", "P", "F"]: From 9b20276c45d7fdae34f24d9a803846b725deba82 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Wed, 30 Oct 2019 07:23:08 +1100 Subject: [PATCH 2/2] Allow for arr KeyError --- Tests/test_image_array.py | 5 +++++ src/PIL/Image.py | 3 +++ 2 files changed, 8 insertions(+) diff --git a/Tests/test_image_array.py b/Tests/test_image_array.py index 02e5c80f2..e2a79764e 100644 --- a/Tests/test_image_array.py +++ b/Tests/test_image_array.py @@ -52,3 +52,8 @@ class TestImageArray(PillowTestCase): self.assertEqual(test("RGB"), ("RGB", (128, 100), True)) self.assertEqual(test("RGBA"), ("RGBA", (128, 100), True)) self.assertEqual(test("RGBX"), ("RGBA", (128, 100), True)) + + # Test mode is None with no "typestr" in the array interface + with self.assertRaises(TypeError): + wrapped = Wrapper(test("L"), {"shape": (100, 128)}) + Image.fromarray(wrapped) diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 0c783a428..4093c030c 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -2631,6 +2631,9 @@ def fromarray(obj, mode=None): if mode is None: try: typekey = (1, 1) + shape[2:], arr["typestr"] + except KeyError: + raise TypeError("Cannot handle this data type") + try: mode, rawmode = _fromarray_typemap[typekey] except KeyError: raise TypeError("Cannot handle this data type: %s, %s" % typekey)