Fastest Way To Loop Through Every Pixel

c d saunter christopher.saunter at durham.ac.uk
Fri Jul 28 06:17:36 EDT 2006


Chaos (psnim2000 at gmail.com) wrote:

: He is the code #Actions here

: myCol = (0.3 * image.GetRed(thisX, thisY)) + (0.59 *
: image.GetGreen(thisX, thisY)) + (0.11 * image.GetBlue(thisX, thisY))
: if myCol < darkestCol:
:    darkestCol = myCol
:    possX = thisX
:    possY = thisY

You really don't want to do this in interpreted Python code.  Way to slow.
I don't know PIL but probably you can do it their.

Another aproach is to use one of the array/maths libraries e.g. Numeric or 
NumPy.

You'll need to move the image data between PIL and the array package with 
the data being a (y,x,3) array for input array and (y,x) for the output 
array.  .tostirng() methods and fromstring() can be usefull here.

Then...

1.  Colour map it
newImage = 0.3 * image[:,:,0] + 0.59 * image[:,:,1] + 0.11 * image[:,:,2]

2. Mask of pixels less than your threshold

newImage = clip(newImage, myCol, MAX_COLOUR)

3. Find the coordinates of the last minimum colour - possX, possY (do you 
really need/mean to do this?) 

hth
cds



More information about the Python-list mailing list