Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

workspace.save_image_file: set dpi on image.save, #343 #611

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions ocrd/ocrd/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We must adapt for the generator now, I believe.

Suggested change
orig_img_file = self.mets.find_files(pageId=page_id, mimetype='//^image')[0]
orig_img_file = next(self.mets.find_files(pageId=page_id, mimetype='//^image', local_only=True))

orig_img_pil = self.resolve_image_exif(orig_img_file.url)
kba marked this conversation as resolved.
Show resolved Hide resolved
exif = OcrdExif(orig_img_pil)
dpi = (exif.xResolution, exif.yResolution)
kba marked this conversation as resolved.
Show resolved Hide resolved
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,
Expand Down