From 59957fb8d8593bf2f87663b8c8c357ea060c8dd5 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 4 May 2020 20:07:23 +1000 Subject: [PATCH] Added support for 1-D NumPy arrays --- Tests/test_numpy.py | 5 +++++ src/PIL/Image.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Tests/test_numpy.py b/Tests/test_numpy.py index 30ab5132a..56addca1b 100644 --- a/Tests/test_numpy.py +++ b/Tests/test_numpy.py @@ -89,6 +89,11 @@ def test_3d_array(): assert_image(Image.fromarray(a[:, :, 1]), "L", TEST_IMAGE_SIZE) +def test_1d_array(): + a = numpy.ones(5, dtype=numpy.uint8) + assert_image(Image.fromarray(a), "L", (1, 5)) + + def _test_img_equals_nparray(img, np): assert len(np.shape) >= 2 np_size = np.shape[1], np.shape[0] diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 0c8b42a09..f500a49d5 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -2727,7 +2727,7 @@ def fromarray(obj, mode=None): if ndim > ndmax: raise ValueError("Too many dimensions: %d > %d." % (ndim, ndmax)) - size = shape[1], shape[0] + size = 1 if ndim == 1 else shape[1], shape[0] if strides is not None: if hasattr(obj, "tobytes"): obj = obj.tobytes()