Image stats - going from Matlab to Python

T

tjv

Hi all,
I am working with images in python using PIL. I come from a MATLAB
background so I am finding it to be sometimes frustrating to do things
correctly and quickly. All I need to do is load an image and store a
list of pixel coordinates at which the alpha channel is set to 1.
In Matlab this would be easy...Lets say we have a 2x2x4 array that
represents the image. I would just type something like:

indices = find(im:),:,3)==1);

then work with indices to get xy coords. Is there a similar way to
accomplish the same thing in python and PIL without having a nested for
loop and checking every pixel?
I would appreciate advice. Thanks very much for your attention!
-Tim
 
R

Robert Kern

Hi all,
I am working with images in python using PIL. I come from a MATLAB
background so I am finding it to be sometimes frustrating to do things
correctly and quickly.

Yup. That's the curse of changing toolsets.
All I need to do is load an image and store a
list of pixel coordinates at which the alpha channel is set to 1.
In Matlab this would be easy...Lets say we have a 2x2x4 array that
represents the image. I would just type something like:

indices = find(im:),:,3)==1);

then work with indices to get xy coords. Is there a similar way to
accomplish the same thing in python and PIL without having a nested for
loop and checking every pixel?
I would appreciate advice. Thanks very much for your attention!

One way would be to use Numeric or numarray, which you will probably
want anyway if you are doing Matlab-type stuff. If you are dealing with
large images or are dealing with raw data on disk (as opposed to PNGs or
GIFs), numarray might be best for you.

A mildly tested example:

import Numeric as N
import Image

filename = 'heavy_1.gif'
img = Image.open(filename).convert('RGBA')
data = img.tostring()

arr = N.fromstring(data, N.UInt8)
arr.shape = (img.size[1], img.size[0], 4)

mask = (arr[:,:,3] == 255).flat

idx = N.indices(arr.shape[:2])
idx.shape = (2, len(mask))

pixels = N.compress(idx, mask)
# for all i, arr[pixels[0,i], pixels[1,i], 3] == 255

I'm sure there's some code floating around that makes these kinds of
operations simpler.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
F

Fernando Perez

Hi all,
I am working with images in python using PIL. I come from a MATLAB
background so I am finding it to be sometimes frustrating to do things
correctly and quickly. All I need to do is load an image and store a
list of pixel coordinates at which the alpha channel is set to 1.
In Matlab this would be easy...Lets say we have a 2x2x4 array that
represents the image. I would just type something like:

indices = find(im:),:,3)==1);

then work with indices to get xy coords. Is there a similar way to
accomplish the same thing in python and PIL without having a nested for
loop and checking every pixel?
I would appreciate advice. Thanks very much for your attention!

The kind of array functionality which you have in mind is implemented in python
by the Numeric/Numarray libraries. You can (and should) use the PIL for the
more image-specific tasks, but basic array things are done via those others.
You may also want to look at matplotlib, for a high level, matlab-compatible
plotting library.

Regards,

f
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,219
Messages
2,571,117
Members
47,727
Latest member
PasqualePf

Latest Threads

Top