[Image-SIG] PIL - 1.1.6, Centroid

Fredrik Lundh fredrik at pythonware.com
Mon Aug 25 20:58:36 CEST 2008


Ashish Asgekar wrote:

>    I was looking for Centroid function in Python and downloaded 
> PIL-1.1.6. I learnt that Centroid function is available as Crack Code. 
> However, when I search for CrackCode, I find it is not available anymore 
> from PythonWare.
>           1) Is CrackCode available in PIL-1.1.6 in some
 >              other form?

the CrackCode extension is no longer available, for licensing reasons.

>           2) Can I compute Centroid of an image using some other 
> existing function?

assuming that "im" contains a single feature (represented by non-zero 
pixels), the following function should do the trick:

def get_centroid(im):
     sx = sy = n = 0
     x0, y0, x1, y1 = im.getbbox()
     pix = im.load()
     for y in range(y0, y1):
         for x in range(x0, x1):
             if pix[x, y]:
                 sx += x
                 sy += y
                 n += 1
     return float(sx) / n + 0.5, float(sy) / n + 0.5

on my machine, the above can process about fourty 512x512 grayscale 
images per second.

</F>



More information about the Image-SIG mailing list