#
# Torbert, 11.16.2009
#
# Demo -- no functionality.
#
# PIL is the Python Image Library.
#

from Tkinter import *
from sys import exit
from PIL import Image,ImageTk
##################################################################
#
# Dimensional parameters -- some of these will change based on the image.
#
w,h,n,pad=800,600,3,1
x,y,dx,dy=50,40,125,125
#
# Representation of the current configuration.
#
rects,objts,tiles=[],[],[]
tx,ty,gx,gy=-1,-1,-1,-1
count,steps=0,100 # steps in the animation
rem_j=None
blank=n*n-1
##################################################################
#
# Callback functions.
#
def quit(evnt):
	exit(0)
##################################################################
#
# Load image.
#
img=Image.open('hulk.jpg')
iw,ih=img.size
w=iw+2*x
h=ih+2*y
dx=iw/n+pad
dy=ih/n+pad
#
# Initialize window.
#
root=Tk()
canvas=Canvas(root,width=w,height=h,bg='white')
canvas.pack()
#
# Create graphics objects. 
#
for r in xrange(n):
	for c in xrange(n):
		x1,y1=x+c*dx   ,y+r*dy
		x2,y2=x+c*dx+dx,y+r*dy+dy
		ix1,iy1=(c+0)*(dx-pad),(r+0)*(dy-pad)
		ix2,iy2=(c+1)*(dx-pad),(r+1)*(dy-pad)
		if r==n-1 and c==n-1:
			imgTk=ImageTk.PhotoImage(Image.new('1',(dx-pad,dy-pad),color=255))
		else:
			imgTk=ImageTk.PhotoImage(img.crop((ix1,iy1,ix2,iy2)))
		objts.append(imgTk)
		rect=canvas.create_image(x1+dx/2,y1+dy/2,image=objts[-1])
		rects.append(rect)
		tiles.append(r*n+c+1)
#
# Register callbacks.
#
root.bind('<q>',quit)
#
# Here we go.
#
root.mainloop()

