diff --git a/ocrd/ocrd/workspace.py b/ocrd/ocrd/workspace.py index eafc136f29..5434fbd6ff 100644 --- a/ocrd/ocrd/workspace.py +++ b/ocrd/ocrd/workspace.py @@ -1002,8 +1002,9 @@ def save_image_file(self, image, file_grp, page_id=None, mimetype='image/png', - force=False): - """Store an image in the filesystem and reference it as new file in the METS. + force=False, + dpi=None): + """Store and reference an image as file into the workspace. Args: image (PIL.Image): derived image to save @@ -1024,7 +1025,17 @@ def save_image_file(self, image, if not force and self.overwrite_mode: force = True image_bytes = io.BytesIO() - image.save(image_bytes, format=MIME_TO_PIL[mimetype]) + if dpi and not isinstance(dpi, tuple): + dpi = (dpi, dpi) + elif not dpi: + # TODO - brittle, will this always find the original image? + orig_img_file = self.mets.find_files(pageId=page_id, mimetype='//^image')[0] + orig_img_pil = self.resolve_image_exif(orig_img_file.url) + exif = OcrdExif(orig_img_pil) + dpi = (exif.xResolution, exif.yResolution) + if exif.resolutionUnit == 'cm': + dpi = (dpi[0] * 2.54, dpi[1] * 2.54) + image.save(image_bytes, dpi=dpi, format=MIME_TO_PIL[mimetype]) file_path = str(Path(file_grp, '%s%s' % (file_id, MIME_TO_EXT[mimetype]))) out = self.add_file( file_grp,