import tkinter as tk from PIL import ImageTk import PIL import math class EPoint: def __init__(self,x,y): self.x=x self.y=y def dist(self,a): return math.sqrt((self.x-a.x)**2+(self.y-a.y)**2) def __str__(self): return "({0},{1})".format(self.x,self.y) def __repr__(self): return "EPoint({0},{1})".format(self.x,self.y) class Application(tk.Frame): def __init__(self, master=None): self.corners=[] tk.Frame.__init__(self, master) self.grid(column=0,row=0) self.createWidgets() def createWidgets(self): # a captured frame with overlay graphics self.imgView=tk.Canvas(self) self.imgView.configure(width=480,height=360) imgOrig=PIL.Image.open("../images/1.jpg") self.img=ImageTk.PhotoImage(imgOrig.resize((int(self.imgView['width']),int(self.imgView['height'])),resample=PIL.Image.BILINEAR)) self.imgView.bind('<1>',lambda e: self.addCorner(e.x,e.y)) self.redrawImgView() self.imgView.grid(column=0,row=0) # board with detected stones self.boardView=BoardView(self) self.boardView.grid(column=1,row=0) def addCorner(self,x,y): a=EPoint(x,y) for i,c in enumerate(self.corners): # move an improperly placed point if a.dist(c)<20: self.corners[i]=a # print(self.corners) self.redrawImgView() return if len(self.corners)<4: # add a new corner self.corners.append(a) # print(self.corners) self.redrawImgView() if len(self.corners)<4: return index,minDist=0,float('inf') # replace the corner closest to the clicked point for i,c in enumerate(self.corners): if a.dist(c)