From 108217303077bdb6b85cf4563b2a3804a61f1d40 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Tue, 21 May 2013 18:35:11 -0300 Subject: [PATCH 1/2] Fix Image.fromarray with NumPy arrays Image.fromarray attempts to call a method called `tobytes()` on the passed in object, but NumPy arrays don't have a `tobytes()` method, they have a `tostring()` method. (See http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tostring.html). I think this was changed accidentally in a Python 3 compatibility update in which this call was confused with the `tobytes` and `frombytes` methods of Image objects. --- PIL/Image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PIL/Image.py b/PIL/Image.py index cafc5a22f..a2a4f444e 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1944,7 +1944,7 @@ def fromarray(obj, mode=None): size = shape[1], shape[0] if strides is not None: - obj = obj.tobytes() + obj = obj.tostring() return frombuffer(mode, size, obj, "raw", rawmode, 0, 1) From c3d6b05d10e0aff043c96c06f97f97dd434c1365 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Tue, 21 May 2013 21:55:58 -0400 Subject: [PATCH 2/2] Adding a test of fromarray that exercises the if strides is not None block. --- Tests/test_numpy.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Tests/test_numpy.py b/Tests/test_numpy.py index 5f8097ef8..b9cacf0bb 100644 --- a/Tests/test_numpy.py +++ b/Tests/test_numpy.py @@ -54,3 +54,11 @@ def test_numpy_to_image(): assert_image(to_image(numpy.uint8, 3), "RGB", (10, 10)) assert_image(to_image(numpy.uint8, 4), "RGBA", (10, 10)) + + +# based on an erring example at http://is.gd/6F0esS +def test_3d_array(): + a = numpy.ones((10, 10, 10), dtype=numpy.uint8) + assert_image(Image.fromarray(a[1, :, :]), "L", (10, 10)) + assert_image(Image.fromarray(a[:, 1, :]), "L", (10, 10)) + assert_image(Image.fromarray(a[:, :, 1]), "L", (10, 10))