473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Image stats - going from Matlab to Python

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

Jul 18 '05 #1
2 2274
tj*@hotmail.com wrote:
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
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
Jul 18 '05 #2
tj*@hotmail.com wrote:
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

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mathias | last post by:
Dear NG, I currently ty to switch from matlab to python/scipy but have a lot of trouble with images. What I need is a function for subsequently displaying a number of 2D-matrices as an image. I...
0
by: Srinath Avadhanula | last post by:
Hello, I wanted to connect to an already existing MATLAB application from python. From the MATHWORKS documentation at: ...
3
by: Ali | last post by:
How to use python in matlab?
4
by: dataangel | last post by:
I'm a student who's considering doing a project for a Machine Learning class on pathing (bots learning to run through a maze). The language primarily used by the class has been Matlab. I would...
9
by: Carl | last post by:
I am desperately looking for a way to call Python from Matlab. I have become used to Python's rich syntax and large number of libraries, and feel ridiculously clumsy being stuck with Matlab's...
4
by: Sven Jerzembeck | last post by:
Hello guys, is there any possibiliy using Phython code in Matlab. I couldnt find any helpful stuff. Its not graphical stuff I am doing just calculations with arrays and strings. Thanks for ...
8
by: Allen | last post by:
Does anyone agree with me? If you have used Matlab, welcome to discuss it.
11
by: Stef Mientki | last post by:
hi All, instead of questions, my first success story: I converted my first MatLab algorithm into Python (using SciPy), and it not only works perfectly, but also runs much faster: MatLab:...
4
by: Claire Mouton | last post by:
Hi, I would like to call Python functions from Matalab. How could I find an interface from Matlab to Python? Cheers, Claire
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.