0

I am using Python 2.7 on Ubuntu and I was experimenting with images before beginning an assignment for my algorithms class. I have downloaded numpy, scipy and skimage packages and I keep getting the error that 'imread' 'title' and these inbuilt functions are undefined. Any idea what could be wrong?

   from pylab import *
   from skimage import img_as_float

   img = imread(‘someimage.png’)
   img = img_as_float(img)
   w, h = img.shape[:2]
   R = img[:,:,0]
   G = img[:,:,1]
   B = img[:,:,2]
   figure()
   gray()
   subplot(1,4,1); imshow(img); title(“RGB”)
   subplot(1,4,2); imshow(R); title(“Red”)
   subplot(1,4,3); imshow(G); title(“Green”)
   subplot(1,4,4); imshow(B); title(“Blue”)
   show()   
Aveesha Sharma
  • 59
  • 2
  • 2
  • 7

3 Answers3

12

Try adding:

from skimage.io import imread

to your imports.

tom10
  • 67,082
  • 10
  • 127
  • 137
  • Tried that. Still comes up with undefined names error. – Aveesha Sharma Oct 20 '14 at 03:35
  • 1
    @AveeshaSharma: If you tried this obvious thing, then why didn't you say that in your question? – tom10 Oct 20 '14 at 03:37
  • I'm sorry. Pylab imports functions from numpy, scipy and skimage. So I didn't realize I had to explicitly mention from skimage.io import imread. By tried I meant, I tried it after reading your post. Didn't work. – Aveesha Sharma Oct 20 '14 at 03:44
  • @AveeshaSharma: Could you show the full traceback that you get to to the code in your question? Also, could you import pylab using `import pylab` and then do `pylab.__file__`? I'm wondering whether you have some other file named `pylab.py` that's being picked up first, and `pylab.__file__` should show which pylab file is being used. – tom10 Oct 20 '14 at 17:13
1

Where is the imread function imported from? You can easily see all functions within an imported library. See- allhttp://stackoverflow.com/questions/139180/listing-all-functions-in-a-python-module

If you are looking for SciPy's imread, you must do it along these lines...

from scipy.misc import imread
...
img = imread(‘someimage.png’)

or

from scipy import misc
...
img = misc.imread('someimage.png')

or

import scipy
... 
img = scipy.misc.imread(‘someimage.png’)

Python does not inherently know what the title function or imread functions/methods are without having it spelled out explicitly. Importing requires you import each function/class/library etc with respect to scope. https://docs.python.org/3/reference/import.html

Also - first comment is correct. You will need PIL installed to use the imread function.

Ian Price
  • 7,416
  • 2
  • 23
  • 34
  • 1
    I tried doing that! from scipy.misc import imread. Still won't work. – Aveesha Sharma Oct 20 '14 at 03:26
  • What won't work? Does imread function show up as undefined or some other function? What is the exact error you get? – Ian Price Oct 20 '14 at 03:30
  • Yes. They get showed up as undefined names. imread title imshow subplot. – Aveesha Sharma Oct 20 '14 at 03:32
  • 1
    @IanPrice Everything you've said is generally true, but `pylab` is a bit of an exception. Pylab imports a whole bunch of functions from numpy, scipy and matplotlib to try and resemble MATLAB. Not necessarily good Python practice, but using Pylab *should* mean that the functions OP is using show up in the global namespace. – Marius Oct 20 '14 at 03:33
  • @Marius Interesting... have used SciPy quite a bit but never Pylab. Thanks for the insight! – Ian Price Oct 20 '14 at 03:35
0

if your facing problem while reading image try this

from matplotlib.pyplot import imread
im = imread(image.png)
Akash Desai
  • 498
  • 5
  • 11