I’m trying to run imsave and imshow and basic scipy functionality dealing with colors in arrays on OS X 10.7 to no avail. Here’s my code, and then following that I’ll post my error. Is there some way to fix or go around this on a permanent basis so I can use this functionality on my mac? I’ve run this identical code on a Linux machine running Ubuntu and have had success.
from numpy import *
from scipy import *
a = zeros((3,500,400))
a[1,0:100,0:200] = 255
imsave('foo.png',a)
If this works correctly I should simply get a black box 500×400 pixels, and a green box inside of that 100×200 pixels in the top left corner.
but instead I get this error
“Traceback (most recent call last):
File “colorstuff.py”, line 10, in
imsave(‘foo.png’,a)
NameError: name ‘imsave’ is not defined”
and a similar error for imshow.
Any ideas on how to go about avoiding this error?
(on a sidenote, i have tried importing from scipy.misc and get the exact same errors)
Edit:
I misread your question. I thought
imsavewasn’t saving your image properly, but that you were able to import it.scipy.misc.imsaveis just a thin wrapper around PIL (the python imaging library). You need to install PIL for it to work.What happens if you just use PIL directly?
scipy.misc.imsavedates from many years ago whenImagedidn’t support numpy arrays. It’s not officially depreciated, but it jumps through a lot of hoops that aren’t necessary with recent versions of PIL.What happens if you do this:
Also, avoid doing
from whatever import *. It’s only there for interactive use. It’s a very bad idea otherwise. (In the case ofnumpy, you’re over-riding many python’s builtin functions with the numpy versions with don’t do the same thing.) Also, don’t use the basescipynamespace. It’s justnumpywith one or two extra functions. All of scipy is in its submodules. Import what you need directly (e.g.from scipy.misc import imsave)