user961627
user961627

Reputation: 12747

Converting image to numpy array in python

I'm using Python + Scipy + Scikit-image + numpy for the first time.

I'm using this page for help, and I've only changed the given code a bit to pick up an image of my liking:

tree = misc.imread('C:\\Users\\app\\Pictures\\treephoto1.jpg')
type(tree)
<type 'numpy.ndarray' >
tree.shape, tree.dtype((512, 512), dtype('uint8'))

But I'm getting the following error:

 type(tree) <type 'numpy.ndarray'>
                                   ^
SyntaxError: invalid syntax

What's wrong with the syntax? I'm using python 2.7 on Windows, and all the related toolkits are also according to Python 2.7.

I need to convert the image to a 2-D numpy array so that I can use the canny edge detector with it.

Upvotes: 0

Views: 2622

Answers (1)

twil
twil

Reputation: 6162

Writing without thinking may be dangerous but in your case it is just wrong. The page you've mentioned shows this:

>>> lena = misc.imread('lena.png')
>>> type(lena)
<type 'numpy.ndarray'>
>>> lena.shape, lena.dtype
((512, 512), dtype('uint8'))

>>> is Python's interactive console prompt string. It is there were commands go.

<type 'numpy.ndarray'> and ((512, 512), dtype('uint8')) are results of commands. So your corresponding code should be only

tree = misc.imread('C:\\Users\\app\\Pictures\\treephoto1.jpg')
type(tree)
tree.shape, tree.dtype

Notice that

type(tree)
tree.shape, tree.dtype

do nothing and just show you information about your image data.

Update

Basically (not always) your image is layered. RGB is three separate layers. So if your filtering isn't aware of it you'll have to separate layers yourself.

Hope that helps.

Upvotes: 3

Related Questions