diff --git a/qurator/eynollah/eynollah.py b/qurator/eynollah/eynollah.py index 197afe9..780371a 100644 --- a/qurator/eynollah/eynollah.py +++ b/qurator/eynollah/eynollah.py @@ -102,6 +102,8 @@ class Eynollah: self._imgs = self._cache_images(image_pil=image_pil) else: self._imgs = self._cache_images(image_filename=image_filename) + if override_dpi: + self.dpi = override_dpi self.image_filename = image_filename self.dir_out = dir_out self.allow_enhancement = allow_enhancement @@ -109,7 +111,6 @@ class Eynollah: self.full_layout = full_layout self.allow_scaling = allow_scaling self.headers_off = headers_off - self.override_dpi = override_dpi self.plotter = None if not enable_plotting else EynollahPlotter( dir_of_all=dir_of_all, dir_of_deskewed=dir_of_deskewed, @@ -138,8 +139,10 @@ class Eynollah: ret = {} if image_filename: ret['img'] = cv2.imread(image_filename) + self.dpi = check_dpi(image_filename) else: ret['img'] = pil2cv(image_pil) + self.dpi = check_dpi(image_pil) ret['img_grayscale'] = cv2.cvtColor(ret['img'], cv2.COLOR_BGR2GRAY) for prefix in ('', '_grayscale'): ret[f'img{prefix}_uint8'] = ret[f'img{prefix}'].astype(np.uint8) @@ -354,7 +357,7 @@ class Eynollah: def resize_and_enhance_image_with_column_classifier(self): self.logger.debug("enter resize_and_enhance_image_with_column_classifier") - dpi = self.override_dpi if self.override_dpi else check_dpi(self.imread()) + dpi = self.dpi self.logger.info("Detected %s DPI", dpi) img = self.imread() diff --git a/qurator/eynollah/processor.py b/qurator/eynollah/processor.py index a9261f3..58d0940 100644 --- a/qurator/eynollah/processor.py +++ b/qurator/eynollah/processor.py @@ -42,12 +42,6 @@ class EynollahProcessor(Processor): # XXX loses DPI information # page_image, _, _ = self.workspace.image_from_page(page, page_id, feature_filter='binarized') self.workspace.download_file(next(self.workspace.mets.find_files(url=page.imageFilename))) - if self.parameter['dpi'] <= 0: - exif = exif_from_filename(page.imageFilename) - dpi = exif.resolution - if exif.resolutionUnit == 'cm': - dpi /= 2.54 - self.parameter['dpi'] = dpi if dpi != 1 else 230 eynollah_kwargs = { 'dir_models': self.resolve_resource(self.parameter['models']), 'allow_enhancement': self.parameter['allow_enhancement'], diff --git a/qurator/eynollah/utils/pil_cv2.py b/qurator/eynollah/utils/pil_cv2.py index 4d35b7a..d6eb0f3 100644 --- a/qurator/eynollah/utils/pil_cv2.py +++ b/qurator/eynollah/utils/pil_cv2.py @@ -15,8 +15,14 @@ def pil2cv(img): return cvtColor(pil_as_np_array, color_conversion) def check_dpi(img): + if isinstance(img, Image.__class__): + pil_image = img + elif isinstance(img, str): + pil_image = Image.open(img) + else: + pil_image = cv2pil(img) try: - exif = OcrdExif(cv2pil(img)) + exif = OcrdExif(pil_image) resolution = exif.resolution if resolution == 1: raise Exception()