Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 20th, 2012  |  syntax: None  |  size: 0.48 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Detect if an image has a border, programmatically (return boolean)
  2. from PIL import Image, ImageChops
  3.  
  4. def is_there_a_border(im):
  5.     bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
  6.     diff = ImageChops.difference(im, bg)
  7.     diff = ImageChops.add(diff, diff, 2.0, -100)
  8.     bbox = diff.getbbox()
  9.     return bbox != (0,0,im.size[0],im.size[1])
  10.        
  11. return all((bbox[0], bbox[1], (bbox[0] + bbox[2]) <= im.size[0],
  12.                                   (bbox[1] + bbox[3]) <= im.size[1]))