diff --git a/src/core.py b/src/core.py --- a/src/core.py +++ b/src/core.py @@ -2,7 +2,9 @@ import os import multiprocessing import threading import logging + import PIL + from util import MsgQueue from gui import gui from analyzer import ImageAnalyzer @@ -10,6 +12,7 @@ from analyzer.framecache import FrameCac from go.core import Go, isLegalPosition from statebag import StateBag import config as cfg +import util log=logging.getLogger(__name__) @@ -25,7 +28,7 @@ class Core: self._ownMessages=MsgQueue(self._handleEvent) self._guiMessages=MsgQueue() - self._imgs=sorted(os.listdir(cfg.misc.imgDir)) + self._imgs=sorted(filter(lambda s: s.endswith(".jpg"), os.listdir(cfg.misc.imgDir))) self._imgIndex=cfg.misc.defaultImage imgPath=os.path.join(cfg.misc.imgDir,self._imgs[self._imgIndex]) self._frame=PIL.Image.open(imgPath) @@ -45,9 +48,10 @@ class Core: def relativeFrame(self,step): self._imgIndex=(self._imgIndex+step)%len(self._imgs) - imgPath=os.path.join(cfg.misc.imgDir,self._imgs[self._imgIndex]) + filename=self._imgs[self._imgIndex] + imgPath=os.path.join(cfg.misc.imgDir,filename) self._frame=PIL.Image.open(imgPath) - self._guiMessages.send("setCurrentFrame",(self._frame.copy(),gui.PREVIEW)) + self._guiMessages.send("setCurrentFrame",(self._frame.copy(),gui.PREVIEW,filename)) self.preview() def sendParams(self): diff --git a/src/gui/__init__.py b/src/gui/__init__.py --- a/src/gui/__init__.py +++ b/src/gui/__init__.py @@ -1,4 +1,6 @@ -import logging +import os +import json +import logging import threading import tkinter as tk @@ -6,6 +8,7 @@ import config from .mainwindow import MainWindow from .boardview import BoardView from .settings import Settings +from util import exportBoard log=logging.getLogger(__name__) @@ -23,6 +26,8 @@ class GUI: self._coreMessages=None self._state=GUI.SETUP + self._board=[] + self._filename="" self.mainWindow = MainWindow(self, master=self.root) self.settings=None @@ -33,6 +38,7 @@ class GUI: self.root.bind("<>", lambda e: self.setUp()) self.root.bind("<>", lambda e: self.setRecording()) self.root.bind("",lambda e: Settings(self)) + self.root.bind("s",lambda e: self._save()) self.mainWindow.bind("",lambda e: self._ownMessages.send("!kill",("gui",))) self.setUp() @@ -62,6 +68,18 @@ class GUI: self.settings.destroy() self.settings=None + def _save(self): + board=exportBoard(self._board) + corners=[[p.x,p.y] for p in self.mainWindow.imgView.exportCorners()] + res={ + self._filename: { + "board": board, + "corners": corners + } + } + with open(os.path.join(config.misc.imgDir,self._filename+".json"), mode="wt") as f: + json.dump(res,f,indent="\t") + def _handleEvent(self,e): actions={ "setCurrentFrame": self._frameHandler, @@ -72,13 +90,15 @@ class GUI: return actions[actionName](*args,**kwargs) - def _frameHandler(self,newFrame,type): + def _frameHandler(self,newFrame,type,filename): if self._state!=type: return + self._filename=filename self.mainWindow.setCurrentFrame(newFrame) self.root.event_generate("<>") def _stateHandler(self,gameState,moves): labels={(row,col):(i+1) for (i,(c,row,col)) in enumerate(moves)} + self._board=gameState self.mainWindow.boardView.redrawState(gameState,labels) def _paramsHandler(self,**params): diff --git a/src/gui/imgview.py b/src/gui/imgview.py --- a/src/gui/imgview.py +++ b/src/gui/imgview.py @@ -31,7 +31,9 @@ class ImgView(ResizableCanvas): self.delete("all") if self._img: - img=self._img.resize((int(self._width),int(self._height))) + w,h=self._img.size + ratio=min(self._width/w, self._height/h) + img=self._img.resize((int(w*ratio),int(h*ratio))) self._tkImg=ImageTk.PhotoImage(img) # just to save the image from garbage collector self.create_image(self._width//2, self._height//2, anchor="center", image=self._tkImg) @@ -65,8 +67,7 @@ class ImgView(ResizableCanvas): # transform corners from show coordinates to real coordinates log.debug(self._corners.corners) self._boardGrid=Grid(self._corners.corners) - corners=[self._transformPoint(c) for c in self._corners.corners] - self._parent.sendMsg("setCorners",(corners,)) + self._parent.sendMsg("setCorners",(self.exportCorners(),)) self.redraw() @@ -75,6 +76,9 @@ class ImgView(ResizableCanvas): self.create_line(x-3,y-3,x+4,y+4,fill="#00ff00") self.create_line(x-3,y+3,x+4,y-4,fill="#00ff00") + def exportCorners(self): + return [self._transformPoint(c) for c in self._corners.corners] + def setUp(self): self.bind('<1>',lambda e: self.addCorner(e.x,e.y)) @@ -95,7 +99,7 @@ class ImgView(ResizableCanvas): wo,ho=self._img.size # o for original widthRatio=wo/w heightRatio=ho/h - self._imgSizeCoef=max(widthRatio,heightRatio) + imgSizeCoef=max(widthRatio,heightRatio) # shift compensates possible horizontal or vertical empty margins from unmatching aspect ratios - self._imgShift=EPoint(wo-w*self._imgSizeCoef,ho-h*self._imgSizeCoef)/2 - return EPoint(self.canvasx(point.x),self.canvasy(point.y)) * self._imgSizeCoef + self._imgShift + imgShift=EPoint(wo-w*imgSizeCoef,ho-h*imgSizeCoef)/2 + return EPoint(self.canvasx(point.x),self.canvasy(point.y)) * imgSizeCoef + imgShift