def image_from_canvas(self, pos: CanvasPos) -> ImagePixelCoordinate: fip = FractionalImagePos.from_CanvasPos(pos, self.image.transform) img = self.image.image img_size = (1, 1) if img: img_size = (img.width, img.height) ip = ImagePixelCoordinate.from_FractionalImagePos(fip, img_size) return ip
def size(self) -> ImagePixelCoordinate: cp1 = CanvasPos(self.left, self.top) cp2 = CanvasPos(self.right, self.bottom) xform = self.images[0].transform img_size = self.images[0].size() fips = [ FractionalImagePos.from_CanvasPos(pos, xform) for pos in (cp1, cp2) ] ipcs = [ ImagePixelCoordinate.from_FractionalImagePos(pos, img_size) for pos in fips ] width = int(round(abs(ipcs[1].x - ipcs[0].x)) + 0.1) height = int(round(abs(ipcs[1].y - ipcs[0].y)) + 0.1) return ImagePixelCoordinate(width, height)
def recenter(self): center_x = 0.5 * (self.right + self.left) center_y = 0.5 * (self.bottom + self.top) if center_x == 0 and center_y == 0: return self._dirty = True # 1) change clip box self.right -= center_x self.left = -self.right self.top -= center_y self.bottom = -self.top # 2) change camera(s) dcp = CanvasPos(center_x, center_y) self.camera.center -= CanvasPos(center_x, center_y) # 3) change image center(s) for img in self.images: fpc = img.transform.center cpc = CanvasPos.from_FractionalImagePos(fpc, img.transform) cpc += dcp fpc = FractionalImagePos.from_CanvasPos(cpc, img.transform) img.transform.center = fpc
def x_fract_from_canvas(self, pos: CanvasPos) -> FractionalImagePos: return FractionalImagePos.from_CanvasPos(pos, self.image.transform)