#!/usr/bin/python # compare.py # Part of the "penpal" project # (Finds Gary Blehm's Identical Penmen) # http://web.mit.edu/wdaher/www/penpal/ # # Waseem Daher # Jan. 21, 2006 import Image, ImageDraw, ImageStat, ImageChops import os, sys import time outfile = 'penmen-comparison.txt' filename = sys.argv[1] data = open(filename, 'r') lines = data.readlines() vars = lines[:11] # Import our variables again # Yeah, this is totally not secure, # but whatever, this isn't a secure # application for line in vars: exec(line) # Now, read in the penmen data penmen = [None] * num_penmen info = lines[13:] for line in info: num,x,y = eval(line) penmen[num] = (x,y) im = Image.open(infile) def getMan(image, centroid, width, height): imgx,imgy = im.size centroidx,centroidy = centroid tlx = max(0, centroidx - width/2) tly = max(0, centroidy - height/2) brx = min(imgx, centroidx + width/2) bry = min(imgy, centroidy + height/2) return image.crop( (tlx,tly,brx,bry) ) def compare(man1, man2): diff = ImageChops.difference(man1, man2) stat = ImageStat.Stat(diff) return stat.sum[0] print "Loading all penmen..." for i in range(num_penmen): penmen[i] = getMan(im, penmen[i], bounding_box_width, bounding_box_height) print "All penmen loaded, beginning comparison..." scores = {} for i in range(num_penmen): for j in range(i+1, num_penmen, 1): myscore = compare(penmen[i], penmen[j]) if scores.has_key(myscore): scores[myscore].append( (i,j) ) else: scores[myscore] = [(i,j)] # Now sort and print out in order of goodness oldStdOut = sys.stdout file = open(outfile, 'w') sys.stdout = file print "Penmen run" print time.asctime() actual_scores = scores.keys() actual_scores.sort() for act in actual_scores: print act, scores[act], "\n" # Restore old stdout sys.stdout = oldStdOut file.close()