def test_data(): """Create NormcapData instance for testing.""" data = NormcapData() data.cli_args = { "verbose": True, "mode": "parse", "lang": "eng+deu", "color": "#FF0000", "path": None, } data.test_mode = True data.top = 0 data.bottom = 10 data.left = 0 data.right = 20 data.words = [ {"line_num": 1, "text": "one"}, {"line_num": 2, "text": "two"}, {"line_num": 2, "text": "three"}, {"line_num": 3, "text": "four"}, ] # Space to check trimming test_img_folder = os.path.dirname(os.path.abspath(__file__)) + "/images/" img = Image.open(test_img_folder + "test_email_magic_1.jpg") data.shots = [{"monitor": 0, "image": img}] data.image = img return data
def handle(self, request: NormcapData) -> NormcapData: """Execute chain of optimizations. Arguments: AbstractHandler {class} -- self request {NormcapData} -- NormCap's session data Returns: NormcapData -- NormCap's session data with optimized image """ self._logger.info("Applying enhancements to image...") # Currently, the image is only enlarged # for other strategies see: https://stackoverflow.com/a/50762612 if request.image: request.image = self._enlarge_dpi(request.image) # request.image = self._grayscale(request.image) # request.image = self._denoise(request.image) # request.image = self._threshold(request.image) # request.image = self._add_padding(request.image) # request.image = self._strech_contrast(request.image) if self._next_handler: return super().handle(request) else: return request
def _crop_image(request: NormcapData) -> NormcapData: """Crop monitor's image and append to session data. Arguments: request {NormcapData} -- NormCap's session data Returns: NormcapData -- Enriched NormCap's session data """ img = request.shots[request.monitor] cropped_img = img["image"].crop( (request.left, request.top, request.right, request.bottom)) request.image = cropped_img return request