diff --git a/iiif_prezi3/helpers/add_thumbnail.py b/iiif_prezi3/helpers/add_thumbnail.py index c0a0b07..0b3838c 100644 --- a/iiif_prezi3/helpers/add_thumbnail.py +++ b/iiif_prezi3/helpers/add_thumbnail.py @@ -1,7 +1,8 @@ from ..loader import monkeypatch_schema from ..skeleton import (AccompanyingCanvas, Annotation, AnnotationCollection, AnnotationPage, Canvas, Collection, Manifest, - PlaceholderCanvas, Range, Reference, ResourceItem, ServiceItem, ServiceItem1) + PlaceholderCanvas, Range, Reference, ResourceItem, + ServiceItem, ServiceItem1) class AddThumbnail: @@ -20,7 +21,7 @@ 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. @@ -70,6 +71,9 @@ def create_thumbnail_from_iiif(self, url, preferred_width=500, **kwargs): if 'sizes' in image_info: new_thumbnail.height = best_fit_size.get('height') new_thumbnail.width = best_fit_size.get('width') + elif 'height' in image_info and 'width' in image_info: + new_thumbnail.height = image_info['height'] + new_thumbnail.width = image_info['width'] if not hasattr(self, 'thumbnail') or self.thumbnail is None: self.thumbnail = [] diff --git a/tests/test_add_thumbnail.py b/tests/test_add_thumbnail.py index 1da84ae..6f583ab 100644 --- a/tests/test_add_thumbnail.py +++ b/tests/test_add_thumbnail.py @@ -66,3 +66,58 @@ def test_create_thumbnail_from_iiif_v3(self, mockrequest_get): # check thumbnail service self.assertEqual(thumbnail.service[0].profile, "level0") self.assertEqual(thumbnail.service[0].type, "ImageService3") + + @patch('iiif_prezi3.helpers.set_hwd_from_iiif.requests.get') + def test_create_thumbnail_from_iiif_v2(self, mockrequest_get): + image_id = 'https://iiif.io/api/image/2.1/example/reference/918ecd18c2592080851777620de9bcb5-gottingen' + image_info_url = f'{image_id}/info.json' + + # successful response with dimensions + mockresponse = Mock(status_code=200) + mockrequest_get.return_value = mockresponse + # set mock to return minimal image api response + mockresponse.json.return_value = { + "@context": "http://iiif.io/api/image/2/context.json", + "@id": "https://iiif.io/api/image/2.1/example/reference/918ecd18c2592080851777620de9bcb5-gottingen", + "height": 3024, + "profile": [ + "http://iiif.io/api/image/2/level1.json", + { + "formats": [ + "jpg", + "png" + ], + "qualities": [ + "default", + "color", + "gray" + ] + } + ], + "protocol": "http://iiif.io/api/image", + "tiles": [ + { + "height": 512, + "scaleFactors": [ + 1, + 2, + 4 + ], + "width": 512 + } + ], + "width": 4032 + } + + thumbnail = self.manifest.create_thumbnail_from_iiif(image_info_url)[0] + + # check thumbnail matches preferred size + self.assertEqual(thumbnail.height, 3024) + self.assertEqual(thumbnail.width, 4032) + + # Since info_json has no sizes, use full/full + self.assertEqual(thumbnail.id, "https://iiif.io/api/image/2.1/example/reference/918ecd18c2592080851777620de9bcb5-gottingen/full/full/0/default.jpg") + + # check thumbnail service + self.assertEqual(thumbnail.service[0].profile, "http://iiif.io/api/image/2/level1.json") + self.assertEqual(thumbnail.service[0].type, "ImageService2")