Skip to content

Instantly share code, notes, and snippets.

@ameerkat
Created November 27, 2011 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ameerkat/1396706 to your computer and use it in GitHub Desktop.
Save ameerkat/1396706 to your computer and use it in GitHub Desktop.
scanned page splitter
from PIL import Image
import math, os, sys
def calculate_seam(im, band_width, middle_bias = True, middle_bias_strength = 2):
pix = im.load()
size = im.size
max_std_dev = 0
max_col = 1
for i in range(band_width, im.size[0]-band_width+1):
multiplier = (1-(4*math.sqrt((i-im.size[0]/2)**2)/im.size[0]/2))**middle_bias_strength
std_dev = 0
for j in range(0, im.size[1]):
# calculate sliding band average
avg_left = 0
for i2 in range(1, band_width+1):
avg_left += pix[i-i2, j]
avg_left /= band_width
avg_right = 0
for i2 in range(0, band_width):
avg_right += pix[i+i2, j]
avg_right /= band_width
# calculate the standard deviation between pixel band averages
std_dev += (avg_right - avg_left)**2
if middle_bias:
#print multiplier
std_dev *= multiplier
std_dev /= im.size[1]
if std_dev > max_std_dev:
max_std_dev = std_dev
max_col = i
return max_col, max_std_dev
def split_save(im, fname, split, outdir = "."):
im1 = im.crop((0, 0, split-1, im.size[1]))
im2 = im.crop((split, 0, im.size[0], im.size[1]))
im1.load()
im2.load()
fp = fname.split('.')
im1.save(outdir+"/"+fp[0]+"_01."+fp[1])
im2.save(outdir+"/"+fp[0]+"_02."+fp[1])
if __name__ == "__main__":
# test script
"""fname = "test.jpg"
im = Image.open(fname)
seam = calculate_seam(im, 20)
split_save(im, fname, seam[0])
"""
status = True
if(len(sys.argv) > 1):
input_dir = sys.argv[1]
output_dir = input_dir+"/"+"split"
if not os.path.exists(output_dir):
os.mkdir(output_dir)
listing = os.listdir(input_dir)
for infile in listing:
im = Image.open(input_dir+"/"+infile)
seam = calculate_seam(im, 20)
split_save(im, infile, seam[0], outdir=output_dir)
if status:
print "[complete] " + infile
else:
print "missing command line parameter: input directory"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment