Skip to content

Commit

Permalink
Init create_thumbnail_from_iiif.
Browse files Browse the repository at this point in the history
  • Loading branch information
markpbaggett committed Dec 4, 2024
1 parent 76d4f40 commit 3c69775
Showing 1 changed file with 75 additions and 1 deletion.
76 changes: 75 additions & 1 deletion iiif_prezi3/helpers/add_thumbnail.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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:
Expand All @@ -20,6 +20,80 @@ def add_thumbnail(self, image_url, **kwargs):
self.thumbnail = list()
self.thumbnail.append(new_thumbnail)
return new_thumbnail

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.
Args:
url (str): An HTTP URL which points at a IIIF Image response.
preferred_width (int, optional): the preferred width of the thumbnail.
**kwargs (): see ResourceItem.
Returns:
list[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', '')
if context == "http://iiif.io/api/image/2/context.json":
if 'sizes' not in image_info:
thumbnail_id = f"{url}/full/full/0/default.jpg"
else:
best_fit_size = min(
image_info.get('sizes'), key=lambda d: abs(d["width"] - preferred_width)
)
thumbnail_id = f"{url}/full/{best_fit_size['width']},{best_fit_size['height']}/0/default.jpg"
profile = next(
(item for item in image_info.get('profile', []) if isinstance(item, str)),
''
)
service = ServiceItem1(
id=image_info['@id'],
profile=profile,
type="ImageService2",
format="image/jpeg"
)
else:
if 'sizes' not in image_info:
thumbnail_id = f"{url}/full/max/0/default.jpg"
service = ServiceItem(
id=image_info['id'],
profile=image_info.get('profile', ''),
type=image_info.get('type', 'ImageService'),
format="image/jpeg"
)
new_thumbnail = ResourceItem(
id=thumbnail_id,
type='Image',
# height=best_fit_size['height'],
# width=best_fit_size['width'],
format="image/jpeg",
**kwargs
)
if not hasattr(self, 'thumbnail') or self.thumbnail is None:
self.thumbnail = []
context = image_info.get('@context', '')
if context == "http://iiif.io/api/image/2/context.json":
profile = next(
(item for item in image_info.get('profile', []) if isinstance(item, str)),
''
)
service = ServiceItem1(
id=image_info['@id'],
profile=profile,
type="ImageService2",
format="image/jpeg"
)
else:
service = ServiceItem(
id=image_info['id'],
profile=image_info.get('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)

0 comments on commit 3c69775

Please sign in to comment.