def prepare_mask(self): img = self.obj.sheet.get_page_image(self.obj.page_number) width, height = self.obj.width, self.obj.height matrix = list(img.recognize.matrix) # Remove any offset from the matrix matrix[4] = 0 matrix[5] = 0 matrix = cairo.Matrix(*matrix) px_width, px_height = matrix.transform_distance(width, height) px_width, px_height = int(math.ceil(px_width)), int( math.ceil(px_height)) surf = cairo.ImageSurface(cairo.FORMAT_A1, px_width, px_height) cr = cairo.Context(surf) cr.set_source_rgba(0, 0, 0, 0) cr.set_operator(cairo.OPERATOR_SOURCE) cr.paint() # Move to center and apply matrix cr.translate(0.5 * px_width, 0.5 * px_height) cr.transform(matrix) cr.set_source_rgba(0, 0, 0, 1) line_width = 1 / 72.0 * 25.4 cr.set_line_width(line_width) matrix.invert() xoff, yoff = matrix.transform_distance(px_width / 2.0, px_height / 2.0) xoff = xoff - width / 2 yoff = yoff - width / 2 return cr, surf, line_width, width, height, xoff, yoff
def prepare_mask(self): img = self.obj.sheet.get_page_image(self.obj.page_number) width, height = self.obj.width, self.obj.height matrix = list(img.recognize.matrix) # Remove any offset from the matrix matrix[4] = 0 matrix[5] = 0 matrix = cairo.Matrix(*matrix) px_width, px_height = matrix.transform_distance(width, height) px_width, px_height = int(math.ceil(px_width)), int(math.ceil(px_height)) surf = cairo.ImageSurface(cairo.FORMAT_A1, px_width, px_height) cr = cairo.Context(surf) cr.set_source_rgba(0, 0, 0, 0) cr.set_operator(cairo.OPERATOR_SOURCE) cr.paint() # Move to center and apply matrix cr.translate(0.5 * px_width, 0.5 * px_height) cr.transform(matrix) cr.set_source_rgba(0, 0, 0, 1) line_width = 1 / 72.0 * 25.4 cr.set_line_width(line_width) matrix.invert() xoff, yoff = matrix.transform_distance(px_width / 2.0, px_height / 2.0) xoff = xoff - width / 2 yoff = yoff - height / 2 return cr, surf, line_width, width, height, xoff, yoff