Python, best way to find out location of a color
Question: A black and white image showing a smile face inside a frame.

[![enter image description
here](http://i.stack.imgur.com/sbqcu.jpg)](http://i.stack.imgur.com/sbqcu.jpg)

What I want is to find out the location of most right point of the smile face.
(in this case, color black shall be at around “184,91” of the image)

By using below I hope to list the colors in the image, then see what can look
for further.

    
    
    from PIL import Image
    im = Image.open("face.jpg")
    print im.convert('RGB').getcolors() # or print im.getcolors()
    

However it returns “None”, and I am stuck.

How can I get the most right point of the face? Thanks.


Answer: As an indirect way, maybe I can: 1\. remove the frame 2\. trim the white

What’s left is the core image itself. The dimension of the image can tell the
coordinates if the core image is of regular pattern.

    
    
    from PIL import Image
    
    img = Image.open("c:\\smile.jpg")
    img2 = img.crop((30, 26, 218, 165))  # remove the frame
    img2.save("c:\\cropped-smile.jpg")
    

[Trim whitespace using PIL](http://stackoverflow.com/questions/10615901/trim-
whitespace-using-pil) teaches how to remove the circle white portions.

    
    
    def trim(im):
        bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
        diff = ImageChops.difference(im, bg)
        diff = ImageChops.add(diff, diff, 2.0, -100)
        bbox = diff.getbbox()
        if bbox:
            return im.crop(bbox)
    
    im = Image.open("c:\\cropped-smile.jpg")
    im = trim(im)
    im.save("c:\\final-smile.jpg")
    

now to get the dimension of the core image.

    
    
    im = Image.open("c:\\final-sbqcu.jpg")
    w, h = im.size
    

I got it as 131,132. So 131,66 shall be the most right point of the image.

