0

I'm experimenting a little bit working with images in Python for a project I'm working on.

This is the first time ever for me programming in Python and I haven't found a tutorial that deals with the issues I'm facing.

I'm experimenting with different image decompositions, and I want to define some variable A as a set image from a specified folder. Basically I'm looking for Python's analog of Matlab's imread.

After googling for a bit, I found many solutions but none seem to work for me for some reason.

For example even this simple code

import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('messi5.jpg',0)

which is supposed to work (taken from http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_gui/py_image_display/py_image_display.html) yields the error "No module named cv2".

Why does this happen? How can I read an image?

Another thing I tried is

import numpy as np
import skimage.io as io
A=io.imread('C:\Users\Oria\Desktop\test.jpg')
io.imshow(A)

which yields the error "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape"

All I want to do is be able to read an image from a specified folder, shouldn't be hard...Should also be noted that the database I work with is ppm files. So I want to read and show ppm images.

Edit: My enviornment is Pyzo. If it matters for anything.

Edit2: Changing the back slashes into forward slashes changes the error to

Traceback (most recent call last):
  File "<tmp 1>", line 3, in <module>
    A=io.imread('C:/Users/Oria/Desktop/test.jpg')
  File "F:\pyzo2015a\lib\site-packages\skimage\io\_io.py", line 97, in imread
    img = call_plugin('imread', fname, plugin=plugin, **plugin_args)
  File "F:\pyzo2015a\lib\site-packages\skimage\io\manage_plugins.py", line 209, in call_plugin
    return func(*args, **kwargs)
  File "F:\pyzo2015a\lib\site-packages\matplotlib\pyplot.py", line 2215, in imread
    return _imread(*args, **kwargs)
  File "F:\pyzo2015a\lib\site-packages\matplotlib\image.py", line 1258, in imread
    'more images' % list(six.iterkeys(handlers.keys)))
  File "F:\pyzo2015a\lib\site-packages\six.py", line 552, in iterkeys
    return iter(d.keys(**kw))
AttributeError: 'builtin_function_or_method' object has no attribute 'keys'
Oria Gruber
  • 1,513
  • 2
  • 22
  • 44
  • 1
    `cv2` isn't something that comes with Python, you have to install it. If the import doesn't work, you've installed it incorrectly. The Unicode error sounds like you have some non-ascii characters in the filename, but without the full error stack it's hard to tell. – Mark Ransom May 08 '15 at 15:10
  • The entire error stack is File "", line 3 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape – Oria Gruber May 08 '15 at 15:12
  • 1
    Change the backward slashes \ in your Windows path to forward slashes /. – MattDMo May 08 '15 at 15:13
  • Tried it now. Please see the edit I made for the result – Oria Gruber May 08 '15 at 15:14
  • @MattDMo good catch, I totally missed that. Could have doubled up on the backslashes to `\\` too. No idea what the new error is about. – Mark Ransom May 08 '15 at 15:34
  • I don't understand how can it be so difficult to do the simplest things... – Oria Gruber May 08 '15 at 15:36

1 Answers1

1

The closest analogue to Matlab's imread is scipy.misc.imread, part of the scipy package. I would write this code as:

import scipy.misc
image_array = scipy.misc.imread('filename.jpg')

Now to your broader questions. The reason this seems hard is because you're coming from Matlab, which uses a different philosophy. Matlab is a monolithic install that comes out of the box with a huge number of functions. Python is modular. The built-in library is relatively small, and then you install packages depending on what you want to do. For instance, the packages scipy (scientific computing), cv2 (computer vision), and PIL (image processing) can all read simple images from disk, so you choose between them depending on what else from the package you might want to use.

This provides a lot more flexibility, but it does require you to become comfortable installing packages. Sadly this is much more difficult on Windows than on Linux-like systems, due to the lack of a "package manager". On Linux I can sudo apt-get install scipy and install all of scipy in one line. In Windows, you might be better off installing something like conda that smooths the package installation process.

cxrodgers
  • 4,317
  • 2
  • 23
  • 29
  • That does not seem to work. I can do import scipy.misc, meaning i already have that package installed, but Pyzo won't let me do scipy.misc.imread. It yields the error "Traceback (most recent call last): File "", line 2, in image_array = scipy.misc.imread('filename.jpg') AttributeError: 'module' object has no attribute 'imread'" – Oria Gruber May 10 '15 at 09:30
  • You might need to install PIL too: http://stackoverflow.com/questions/15345790/module-has-no-attribute-imread – cxrodgers May 10 '15 at 23:03