#!/usr/bin/python # Crop notebook pages from the book archiver to just the page # from PIL import Image, ImageDraw from math import sqrt import os.path import sys, getopt #Color point settings white_pt = (150, 150, 150) brown_pt= (98, 75, 60) #Minimum number of paper pixels in a row/column to keep that row/col keepThreshold = 50 # calculate distance in RGB color space def colorDist(pt_a, pt_b): dx = pt_a[0] - pt_b[0] dy = pt_a[1] - pt_b[1] dz = pt_a[2] - pt_b[2] return sqrt((dx * dx) + (dy * dy) + (dz * dz)) # get the top and bottom crop rows def getHorizCrop(scan_range, height, pix): for x in scan_range: white_count = 0 for y in range(0, height): if colorDist(pix[x,y], white_pt) < colorDist(pix[x,y], brown_pt): #It's a white pixel white_count += 1 if white_count > keepThreshold: return x #get the left and right crop columns def getVertCrop(scan_range, width, pix): for y in scan_range: white_count = 0 for x in range(0, width): if colorDist(pix[x,y], white_pt) < colorDist(pix[x,y], brown_pt): #It's a white pixel white_count += 1 if white_count > keepThreshold: return y def isImage(filename): try: i=Image.open(filename) return i.format in ('JPEG', 'GIF', 'PNG') except IOError: return False def autoCrop(imageFileName): im = Image.open(imageFileName) pix = im.load() width, height = im.size; left = getHorizCrop(range(0,width), height, pix) top = getVertCrop(range(0,height), width, pix) right = getHorizCrop(range(width-1, 0, -1), height, pix) bottom = getVertCrop(range(height-1, 0, -1), width, pix) # Crop the box defined by ((left, top), (right, bottom)) im = im.crop((left, top, right, bottom)) #Save the image prefix, ext = os.path.splitext(imageFileName) im.save(prefix + "_cropped" + ext) def main(): try: opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) except getopt.error, msg: print msg print "To get help use -h or --help" sys.exit(2) for o, a in opts: if o in ("-h", "--help"): print "There's just no helping some people." sys.exit(0) for arg in args: if isImage(arg): autoCrop(arg) else: print arg + " is not a directory, skipped." if __name__ == "__main__": main()