简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM