do DPI calculation as part of caching images

pull/33/head
Konstantin Baierer 4 years ago
parent ae0b4a825a
commit d0b0e23ac6

@ -102,6 +102,8 @@ class Eynollah:
self._imgs = self._cache_images(image_pil=image_pil) self._imgs = self._cache_images(image_pil=image_pil)
else: else:
self._imgs = self._cache_images(image_filename=image_filename) self._imgs = self._cache_images(image_filename=image_filename)
if override_dpi:
self.dpi = override_dpi
self.image_filename = image_filename self.image_filename = image_filename
self.dir_out = dir_out self.dir_out = dir_out
self.allow_enhancement = allow_enhancement self.allow_enhancement = allow_enhancement
@ -109,7 +111,6 @@ class Eynollah:
self.full_layout = full_layout self.full_layout = full_layout
self.allow_scaling = allow_scaling self.allow_scaling = allow_scaling
self.headers_off = headers_off self.headers_off = headers_off
self.override_dpi = override_dpi
self.plotter = None if not enable_plotting else EynollahPlotter( self.plotter = None if not enable_plotting else EynollahPlotter(
dir_of_all=dir_of_all, dir_of_all=dir_of_all,
dir_of_deskewed=dir_of_deskewed, dir_of_deskewed=dir_of_deskewed,
@ -138,8 +139,10 @@ class Eynollah:
ret = {} ret = {}
if image_filename: if image_filename:
ret['img'] = cv2.imread(image_filename) ret['img'] = cv2.imread(image_filename)
self.dpi = check_dpi(image_filename)
else: else:
ret['img'] = pil2cv(image_pil) ret['img'] = pil2cv(image_pil)
self.dpi = check_dpi(image_pil)
ret['img_grayscale'] = cv2.cvtColor(ret['img'], cv2.COLOR_BGR2GRAY) ret['img_grayscale'] = cv2.cvtColor(ret['img'], cv2.COLOR_BGR2GRAY)
for prefix in ('', '_grayscale'): for prefix in ('', '_grayscale'):
ret[f'img{prefix}_uint8'] = ret[f'img{prefix}'].astype(np.uint8) 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): def resize_and_enhance_image_with_column_classifier(self):
self.logger.debug("enter resize_and_enhance_image_with_column_classifier") 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) self.logger.info("Detected %s DPI", dpi)
img = self.imread() img = self.imread()

@ -42,12 +42,6 @@ class EynollahProcessor(Processor):
# XXX loses DPI information # XXX loses DPI information
# page_image, _, _ = self.workspace.image_from_page(page, page_id, feature_filter='binarized') # 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))) 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 = { eynollah_kwargs = {
'dir_models': self.resolve_resource(self.parameter['models']), 'dir_models': self.resolve_resource(self.parameter['models']),
'allow_enhancement': self.parameter['allow_enhancement'], 'allow_enhancement': self.parameter['allow_enhancement'],

@ -15,8 +15,14 @@ def pil2cv(img):
return cvtColor(pil_as_np_array, color_conversion) return cvtColor(pil_as_np_array, color_conversion)
def check_dpi(img): 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: try:
exif = OcrdExif(cv2pil(img)) exif = OcrdExif(pil_image)
resolution = exif.resolution resolution = exif.resolution
if resolution == 1: if resolution == 1:
raise Exception() raise Exception()

Loading…
Cancel
Save