-
Notifications
You must be signed in to change notification settings - Fork 12
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
Create Thumbnail from IIIF #209
Open
markpbaggett
wants to merge
18
commits into
iiif-prezi:main
Choose a base branch
from
markpbaggett:thumbnail_experiments
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3c69775
Init create_thumbnail_from_iiif.
markpbaggett 73a9573
Make sure everything works with level 0.
markpbaggett aeb6e92
Init thumbnail tests.
markpbaggett 8db7358
Fix subscription problem.
markpbaggett 9c16236
Add v2 test.
markpbaggett e4eab7d
Fix griffe warning.
markpbaggett 764aabd
Add level0 tests.
markpbaggett aed113a
If level0 && sizes and v2, only width.
markpbaggett daf26ae
Since max, default to smallest.
markpbaggett 378bc0a
Update tests.
markpbaggett 236ac54
Update messy code with passing tests before refactor.
markpbaggett a1f442e
Make docblock pass D205 and D415.
markpbaggett 094ab20
Slight refactor.
markpbaggett 960eb2a
Try tackling with 2 private methods.
markpbaggett b86b2d2
Nuke unused method.
markpbaggett 167a185
Make profiles a tuple.
markpbaggett 280570f
Use private method for profile.
markpbaggett 2cc2f97
If profile_data is list, ensure it's not empty.
markpbaggett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
from ..loader import monkeypatch_schema | ||
from ..skeleton import (AccompanyingCanvas, Annotation, AnnotationCollection, | ||
AnnotationPage, Canvas, Collection, Manifest, | ||
PlaceholderCanvas, Range, Reference, ResourceItem) | ||
PlaceholderCanvas, Range, Reference, ResourceItem, | ||
ServiceItem, ServiceItem1) | ||
|
||
|
||
class AddThumbnail: | ||
|
@@ -21,5 +22,104 @@ def add_thumbnail(self, image_url, **kwargs): | |
self.thumbnail.append(new_thumbnail) | ||
return new_thumbnail | ||
|
||
def __get_best_fit_size(self, image_info, preferred_width): | ||
if 'sizes' in image_info: | ||
return min( | ||
(size for size in image_info['sizes'] if size["width"] >= preferred_width), | ||
key=lambda size: size["width"], | ||
default=image_info['sizes'][-1] | ||
) | ||
else: | ||
return None | ||
|
||
monkeypatch_schema([Canvas, PlaceholderCanvas, AccompanyingCanvas, AnnotationPage, Collection, Manifest, Annotation, Range, ResourceItem, AnnotationCollection, Reference], AddThumbnail) | ||
def __get_thumbnail_id(self, url, image_info, best_fit, preferred_width, aspect_ratio): | ||
context = image_info.get('@context', '') | ||
profile_data = image_info.get('profile', []) | ||
if isinstance(profile_data, list): | ||
profile = next((item for item in profile_data if isinstance(item, str)), '') | ||
else: | ||
profile = profile_data | ||
base_url = url.replace('/info.json', '') | ||
if best_fit: | ||
width = best_fit['width'] | ||
height = best_fit.get('height', '') | ||
if context == "http://iiif.io/api/image/2/context.json": | ||
return f"{base_url}/full/{width},/0/default.jpg" | ||
else: | ||
return f"{base_url}/full/{width},{height}/0/default.jpg" | ||
level_0_profiles = {"http://iiif.io/api/image/2/level0.json", "level0"} | ||
if profile not in level_0_profiles: | ||
if context == "http://iiif.io/api/image/2/context.json": | ||
return f"{base_url}/full/{preferred_width},/0/default.jpg" | ||
else: | ||
height = int(preferred_width / aspect_ratio) | ||
return f"{base_url}/full/{preferred_width},{height}/0/default.jpg" | ||
if context == "http://iiif.io/api/image/2/context.json": | ||
return f"{base_url}/full/full/0/default.jpg" | ||
else: | ||
return f"{base_url}/full/max/0/default.jpg" | ||
|
||
def create_thumbnail_from_iiif(self, url, preferred_width=500, **kwargs): | ||
"""Adds an image thumbnail to a manifest or canvas based on a IIIF service. | ||
|
||
If there is a sizes property, it returns the thumbnail that is closest to the preferred width but larger. If no | ||
sizes property exists and it's not level zero image service, it returns a thumbnail with the exact preferred | ||
width. Else, it returns the level `full/full` or `full/max`. | ||
|
||
Args: | ||
url (str): An HTTP URL which points at a IIIF Image response. | ||
glenrobson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
preferred_width (int, optional): the preferred width of the thumbnail. | ||
glenrobson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
**kwargs (): see ResourceItem. | ||
|
||
Returns: | ||
new_thumbnail (ResourceItem): The updated list of thumbnails, including the newly-created one. | ||
""" | ||
image_response = ResourceItem(id=url, type='Image') | ||
image_info = image_response.set_hwd_from_iiif(url) | ||
context = image_info.get('@context', '') | ||
aspect_ratio = image_info.get('width') / image_info.get('height') | ||
profile_data = image_info.get('profile', []) | ||
profile = ( | ||
profile_data if isinstance(profile_data, str) | ||
else next((item for item in profile_data if isinstance(item, str)), '') | ||
if isinstance(profile_data, list) else '' | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this to private method as its also done in the get_thumbnail method |
||
best_fit_size = self.__get_best_fit_size(image_info, preferred_width) | ||
thumbnail_id = self.__get_thumbnail_id(url, image_info, best_fit_size, preferred_width, aspect_ratio) | ||
|
||
new_thumbnail = ResourceItem(id=thumbnail_id, type='Image', format="image/jpeg", **kwargs) | ||
if best_fit_size: | ||
new_thumbnail.width = best_fit_size['width'] | ||
new_thumbnail.height = best_fit_size['height'] | ||
else: | ||
new_thumbnail.width = preferred_width | ||
new_thumbnail.height = int(preferred_width / aspect_ratio) | ||
|
||
if not hasattr(self, 'thumbnail') or self.thumbnail is None: | ||
self.thumbnail = [] | ||
|
||
service = ( | ||
ServiceItem1( | ||
id=image_info['@id'], | ||
profile=profile, | ||
type="ImageService2", | ||
format="image/jpeg" | ||
) | ||
if context == "http://iiif.io/api/image/2/context.json" | ||
else ServiceItem( | ||
id=image_info['id'], | ||
profile=profile, | ||
type=image_info.get('type', 'ImageService'), | ||
format="image/jpeg" | ||
) | ||
) | ||
new_thumbnail.add_service(service) | ||
self.thumbnail.append(new_thumbnail) | ||
return self.thumbnail | ||
|
||
|
||
monkeypatch_schema( | ||
[Canvas, PlaceholderCanvas, AccompanyingCanvas, AnnotationPage, Collection, Manifest, Annotation, | ||
Range, ResourceItem, AnnotationCollection, Reference], | ||
AddThumbnail | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simplify to profile_data[0] if its a list