Files
@ 52d1a214c032
Branch filter:
Location: OneEye/src/gui/imgview.py
52d1a214c032
3.1 KiB
text/x-python
refactoring and documenting API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | import logging
from PIL import ImageTk
import config
from .resizablecanvas import ResizableCanvas
from analyzer.corners import Corners
from analyzer.epoint import EPoint
from analyzer.grid import Grid
import analyzer
log=logging.getLogger(__name__)
class ImgView(ResizableCanvas):
def __init__(self,master=None,parent=None):
super().__init__(master)
self._parent=parent
self._corners=Corners()
self._boardGrid=None
self._img=None
self._tkImg=None
self.configure(width=480,height=360)
self.bind('<1>',lambda e: self.addCorner(e.x,e.y))
## Redraws the current image and its overlay.
def redraw(self):
self.delete("all")
if self._img:
img=self._img.resize((int(self._width),int(self._height)))
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)
for corner in self._corners.corners:
self.markPoint(corner.x,corner.y)
if self._boardGrid!=None and config.gui.showGrid:
for r in range(19):
a=self._boardGrid.intersections[r][0]
b=self._boardGrid.intersections[r][-1]
self.create_line(a.x,a.y,b.x,b.y,fill='#00ff00')
for c in range(19):
a=self._boardGrid.intersections[0][c]
b=self._boardGrid.intersections[-1][c]
self.create_line(a.x,a.y,b.x,b.y,fill='#00ff00')
if self._boardGrid!=None and config.gui.showBigPoints:
for r in range(19):
for c in range(19):
((r1,c1),(r2,c2))=analyzer.relevantRect(self._boardGrid.intersections[r][c], *(self._boardGrid.stoneSizeAt(r, c)))
self.create_rectangle(r1,c1,r2,c2,outline="#00ffff")
def setImg(self,img):
self._img=img
## Stores a grid corner located at x,y coordinates.
def addCorner(self,x,y):
self._corners.add(x,y)
log.debug("click on %d,%d",x,y)
if self._corners.canonizeOrder():
# 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.detector.setCorners(corners)
self.redraw()
## Marks a point at the image with a green cross. Used for corners.
def markPoint(self,x,y):
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 setUp(self):
self.bind('<1>',lambda e: self.addCorner(e.x,e.y))
def setRecording(self):
self.bind('<1>',lambda e: None)
def isSet(self):
return self._img is not None
def _onResize(self,event):
w=self._width
super()._onResize(event)
self._corners.scale(self._width/w)
if len(self._corners.corners)==4:
self._boardGrid=Grid(self._corners.corners)
self.redraw()
def _transformPoint(self,point):
w=int(self._width)
h=int(self._height)
wo,ho=self._img.size # o for original
widthRatio=wo/w
heightRatio=ho/h
self._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
|