Detect if an image has a border, programmatically (return boolean)
Question: First off, I have read this post. [How to detect an image border
programmatically?](http://stackoverflow.com/questions/1954742/how-to-detect-
an-image-border-programmatically) He seems to be asking a slightly different
question, on finding the X/Y coordinates though.

I am just trying to find whether or not a **solid** border exists around a
given photo. I've explored using ImageMagick, but is this the best option?
I've never done any Image-related programming so I'm hoping there's just a
simple api out there that can solve this problem. I'm also fairly new to how
to use these libraries, so any advice is appreciated. I'd prefer solutions in
Python or Java, anything is fine though.

Thanks!


Answer: I answered a related question
[here](http://stackoverflow.com/questions/10615901/trim-whitespace-using-
pil/10616717#10616717), that removes any border around an image, it uses
[PIL](http://www.pythonware.com/products/pil/). You can easily adapt the code
so it returns `True` or `False` for whether there is a border or not, like
this:

    
    
    from PIL import Image, ImageChops
    
    def is_there_a_border(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()
        return bbox != (0,0,im.size[0],im.size[1])
    

However, this will return `True` even if only one side of an image has a
border. But it sounds like you want to know if there is a border all the way
around an image. To do that change the last line to:

    
    
        return all((bbox[0], bbox[1], (bbox[0] + bbox[2]) <= im.size[0], 
                                      (bbox[1] + bbox[3]) <= im.size[1]))
    

This only returns true if there is a border on every side.

For example:

`False:`

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

`False:`

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

`True:`

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

