From fcb3503dec35e9fa88815dc8690b6d2e4515c1cd Mon Sep 17 00:00:00 2001 From: Konstantin Baierer Date: Mon, 21 Sep 2020 18:13:44 +0200 Subject: [PATCH 1/2] workspace.save_image_file: set dpi on image.save, #343 --- ocrd/ocrd/workspace.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ocrd/ocrd/workspace.py b/ocrd/ocrd/workspace.py index 355b014d0d..6406a04258 100644 --- a/ocrd/ocrd/workspace.py +++ b/ocrd/ocrd/workspace.py @@ -836,6 +836,7 @@ def image_from_segment(self, segment, parent_image, parent_coords, def save_image_file(self, image, file_id, file_grp, + dpi=None, page_id=None, mimetype='image/png', force=False): @@ -851,7 +852,15 @@ 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) + 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( ID=file_id, From 552a7c96c06108b436ca311b757e07e0b16a0ba8 Mon Sep 17 00:00:00 2001 From: Konstantin Baierer Date: Tue, 22 Sep 2020 12:41:41 +0200 Subject: [PATCH 2/2] dpi on save_image_file: reorder args, account for resolutionUnit == "cm" --- ocrd/ocrd/workspace.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ocrd/ocrd/workspace.py b/ocrd/ocrd/workspace.py index 6406a04258..821acbab79 100644 --- a/ocrd/ocrd/workspace.py +++ b/ocrd/ocrd/workspace.py @@ -836,10 +836,10 @@ def image_from_segment(self, segment, parent_image, parent_coords, def save_image_file(self, image, file_id, file_grp, - dpi=None, page_id=None, mimetype='image/png', - force=False): + force=False, + dpi=None): """Store and reference an image as file into the workspace. Given a PIL.Image `image`, and an ID `file_id` to use in METS, @@ -860,6 +860,8 @@ def save_image_file(self, image, 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(