from PIL import Image,ImageTk from math import * from commands import getoutput from Tkinter import Tk,Canvas from multiprocessing import Pool layer_thickness = 7.5 dir_to_load = "small_align_test/" contents = [dir_to_load + i for i in getoutput("ls " + dir_to_load).split()] stack = [Image.open(fn).convert("L") for fn in contents] width, height = stack[0].size print width, height # # pix = im.load() # current_image = 0 max_distance = 30 app = Tk() canvas = Canvas(app) canvas.pack() canvas['width'] = 1280 canvas['height'] = 960 pimg = ImageTk.PhotoImage(image=stack[0]) canvas.create_image(0, 0, anchor='center', image=pimg) def changeimages(event, direction=1): global pimg, current_image current_image += direction if(current_image >= len(stack)): current_image = 0 if(current_image < 0): current_image = len(stack)-1 pimg = ImageTk.PhotoImage(image=stack[current_image]) canvas.create_image(0, 0, anchor='nw', image=pimg) canvas.bind("", changeimages) canvas.bind("", lambda e : changeimages(e, -1)) def compare_with_next(arg): i, xoff, yoff = arg diff = 0 for x in range(width): if x < -xoff or x+xoff >= width: continue for y in range(height): if y < -yoff or y+yoff >= height: continue diff += abs(stack[i].getpixel((x,y)) - stack[i+1].getpixel((x + xoff, y + yoff))) return diff from sys import exit for i in range(len(stack)-1): offset_best = float(compare_with_next((i, 0, 0 ))) best_offset = (0, 0) for xoff in range(-max_distance, max_distance): yparam = [(i, xoff, yoff) for yoff in range(-max_distance, max_distance)] pool = Pool() yres = pool.map(compare_with_next, yparam) pool.close() pool.join() best_yoff = -max_distance best_yoff_diff = yres[0] for yoff in range(-max_distance, max_distance): diff = yres[yoff + max_distance] if(diff < offset_best): pixels = width*height - abs(xoff) * height - abs(yoff) * (width - abs(xoff)) offset_best = float(diff) / float(pixels) best_offset = (xoff, yoff) if(best_yoff_diff > diff): best_yoff_diff = diff best_yoff = yoff print "For yoff ", yoff, " best xoff = ", best_yoff, "(", best_yoff_diff, ")" print "\ni = ", i, "Best offset: ", best_offset, " diff = ", offset_best #app.mainloop()