-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Image.fromarray with NumPy arrays #224
Conversation
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.
The python 2.6 (and only 2.6) test run is failing (https://travis-ci.org/python-imaging/Pillow/builds/7372719) with:
Which indicates that in (only) the python 2.6 case, we're getting an image object there instead of ndarray. I'm a bit confused with this, as these tests were passing prior, so either we're not testing Numpy adequately to catch a basic problem or we don't have a problem. That one test is failing now points to inadequate tests. Could you add a test that fails before, and succeeds after your patch? |
So |
I don't know, but my gut reaction is that we really should be expecting one type of object there. However, prior to changes I'd like real understanding, rather than a gut feeling. Also tests. What I don't understand is that if this is patch is correct, why does the test_numpy.py set of tests work even without it. There's a fromarray call there:
|
Interesting, I'll have a look. |
I ran this through coverage and the reason the tests don't discover this is that none of them actually trigger the |
I've added a test to test_numpy.py that trips the |
Ok. Looking at the details here, it appears that we're getting warnings on Py2.6 for the tostring that happens from test_image_array.py:28. We're not getting warnings from Py2.7 from apparently the same object calling the tostring. (as checked in pdb).
What's more, it appears that warnings aren't being generated by default on (Ubuntu 12.04 system python) py2.7:
Where on 2.6, (Ubuntu 10.04 system python) there is a warning:
So, our travis builds aren't generating the warnings, except on 2.6. This is suboptimal. So that explains the python 2.6 difference. So, the stated requirement of the function is that obj provides an array interface, the array_interface spec doesn't say anything about tostring or tobytes. So, I'd say we should duck type it. Check for tobytes, fall back to tostring. I've checked https://github.com/wiredfool/Pillow/tree/pr224 against python 2.6, and it's passing Travis on all the versions with the ducktyping in. |
Thanks for fixing this. I work with @jiffyclub and was confused why this was working on my laptop (Ubuntu 12.04, 2.7) but not on my work machine (OSX 10.6, 2.7), your explanation makes sense. Thanks again. |
The original PIL still has |
Fix Image.fromarray with NumPy arrays: Supersedes PR #224
Image.fromarray attempts to call a method called
tobytes()
on the passed in object, but NumPy arrays don't have atobytes()
method, they have atostring()
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
andfrombytes
methods of Image objects.