from PIL import Image,ImageTk from math import * from commands import getoutput from Tkinter import Tk,Canvas from thread import start_new_thread from time import sleep layer_thickness = 7.5 image_dir = "small_align_test/" contents = [image_dir + i for i in getoutput("ls " + image_dir).split()] stack = [Image.open(fn).convert("L") for fn in contents] offset = [(-3, -9), (-40, -40)] cot = [0,0] for i in range(len(offset)): adjusted_offset = (cot[0] + offset[i][0], cot[1] + offset[i][1]) offset[i] = adjusted_offset cot[0] += offset[i][0] cot[1] += offset[i][1] #im = Image.open("/home/seth/B1run02_png/B1_Run02_BSED_slice_0176.png") width, height = stack[0].size print width, height # # pix = im.load() # current_image = 0 app = Tk() canvas = Canvas(app) canvas.pack() canvas['width'] = 1280 canvas['height'] = 960 pimg = ImageTk.PhotoImage(image=stack[0]) def refresh_image(): global pimg pimg = ImageTk.PhotoImage(image=stack[current_image]) if current_image == 0: canvas.create_image(100, 100, anchor='center', image=pimg) else: xoff, yoff = offset[current_image - 1] canvas.create_image(100 + xoff, 100 - yoff, anchor='center', image=pimg) canvas.create_image(100, 100, 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 refresh_image() canvas.bind("", changeimages) canvas.bind("", lambda e : changeimages(e, -1)) def floodfill(event): img_x = event.x # ONLY when the image is anchored in the corner img_y = event.y inside = 1 frontier = [(img_x,img_y, current_image)]; while len(frontier) > 0: img_x, img_y, ci = frontier.pop() neighbors = [ # (img_x,img_y, ci + 1), # (img_x,img_y, ci - 1), (img_x + 1,img_y, ci), (img_x - 1,img_y, ci), (img_x, img_y + 1, ci), (img_x, img_y - 1, ci) ]; for n in neighbors: nx, ny, nz = n if nz < 0 or nz >= len(stack): continue if nx < 0 or nx >= width: continue if ny < 0 or ny >= height: continue c = stack[nz].getpixel((nx, ny)); if(c > 130 and c != 255): frontier.append(n) stack[nz].putpixel((nx, ny), 255) inside += 1 # pix = stack[nz].load() # pix[nx, ny] = 255 if inside % 10000 == 0: print "Frontier: ", len(frontier) if inside % 100000 == 0: refresh_image() print "*********** Refresh ****************" refresh_image() canvas.bind("", lambda e : start_new_thread(floodfill, (e,))) def reportval(event): print "Value at (", event.x, "\b,", event.y, "\b) =", stack[current_image].getpixel((event.x, event.y)) def highlight_boundaries(event): new_image = Image.new("L", (width, height)) newpix = new_image.load() for x in range(2, 1000-2): for y in range(2, 1000 - 2): neighbors = [ (x + 1,y), (x - 1,y), (x, y + 1 ), (x, y - 1 ), (x + 2,y), (x - 2,y), (x, y + 2 ), (x, y - 2 ), (x + 1,y + 1), (x - 1,y - 1), (x - 1, y + 1 ), (x + 1, y - 1 ) ]; nvals = [stack[current_image].getpixel((n[0], n[1])) for n in neighbors] our_color = stack[current_image].getpixel((x, y)) navg = sum(nvals)/len(nvals) diff = sum([abs(nv - navg) for nv in nvals]) / 3 print diff newpix[x,y] = 50 + our_color - diff #newpix[x,y] = diff if(newpix[x,y] < 0): newpix[x,y] = 0 if(newpix[x,y] > 255): newpix[x,y] = 255 stack[current_image] = new_image refresh_image() canvas.bind("", highlight_boundaries) #canvas.bind("", reportval); ball_x = 300 ball_y = 300 the_ball = 0 spot_size = 20 def make_circle(event): global ball_x, ball_y, the_ball ball_x = event.x ball_y = event.y if(the_ball != 0): # This should have a semaphore canvas.delete(the_ball) the_ball = canvas.create_oval(ball_x-spot_size, ball_y-spot_size, ball_x+spot_size, ball_y+spot_size, fill="#FF00FF") canvas.bind("", make_circle) def animation_loop(): global ball_y, the_ball while True: if(the_ball != 0): canvas.delete(the_ball) ball_y += 5 the_ball = canvas.create_oval(ball_x-spot_size, ball_y-spot_size, ball_x+spot_size, ball_y+spot_size, fill="blue") canvas.update() sleep(0.1) #start_new_thread(animation_loop, ()) def key_press(event): print event print "Key press" def key_release(event): print "Key release" app.bind("", key_press) app.bind("", key_press) def resize(event): global spot_size spot_size += 1 app.bind("", resize) app.mainloop()