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

refactor AlternativeImage selection logic out of Workspace into a stateless function (without downloading) #1301

Open
bertsky opened this issue Jan 20, 2025 · 1 comment

Comments

@bertsky
Copy link
Collaborator

bertsky commented Jan 20, 2025

we need to know whether refactoring the AlternativeImage selection logic out of Workspace.image_from_* into a stateless function (without any download_file behaviour) would break any existing API in the future, hence whether it must be done prior to 3.0 or can be done later.

@kba I don't think we need to break anything here in the future. The methods Workspace.image_from_page and Workspace.image_from_segment could be re-implemented as follows:

  • delegate to new generateds user methods PageType.get_image and [*Region|TextLine|Word]Type.get_image,
    but pass as new kwarg resolve a function with the following definition:
    def resolve(image_url):
        try:
            f = next(self.mets.find_files(local_filename=str(image_url)))
            return f.local_filename
        except StopIteration:
            try:
                f = next(self.mets.find_files(url=str(image_url)))
                return self.download_file(f).local_filename
            except StopIteration:
                with download_temporary_file(image_url) as f:
                   return f.name
  • replace calls to resolve_image_exif by calls to exif_from_filename directly,
    but allow overriding filename via resolve
  • replace calls to resolve_image_as_pil by calls to a new function image_from_filename,
    which merely contains the parts that do Image.open() and .load() to give up the FD, as well as array conversion for the badly supported color modes I and F,
    but allow overriding filename via resolve

Originally posted by @bertsky in bertsky#21 (comment)

@bertsky
Copy link
Collaborator Author

bertsky commented Jan 20, 2025

related: #264

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant