-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from iiif-prezi/issue-10
Create canvas from IIIF Image
- Loading branch information
Showing
7 changed files
with
205 additions
and
10 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
|
||
from ..loader import monkeypatch_schema | ||
from ..skeleton import (Annotation, AnnotationPage, Canvas, Manifest, | ||
ResourceItem, ServiceItem, ServiceItem1) | ||
|
||
|
||
class CreateCanvasFromIIIF: | ||
# should probably be added to canvas helpers | ||
|
||
def create_canvas_from_iiif(self, url, **kwargs): | ||
"""Create a canvas from a IIIF Image URL. | ||
Creates a canvas from a IIIF Image service passing any | ||
kwargs to the Canvas. Returns a Canvas object | ||
""" | ||
canvas = Canvas(**kwargs) | ||
|
||
body = ResourceItem(id="http://example.com", type="Image") | ||
infoJson = body.set_hwd_from_iiif(url) | ||
|
||
# Will need to handle IIIF 2... | ||
if 'type' not in infoJson: | ||
# Assume v2 | ||
|
||
# V2 profile contains profile URI plus extra features | ||
profile = '' | ||
for item in infoJson['profile']: | ||
if isinstance(item, str): | ||
profile = item | ||
break | ||
|
||
service = ServiceItem1(id=infoJson['@id'], profile=profile, type="ImageService2") | ||
body.service = [service] | ||
body.id = f'{infoJson["@id"]}/full/full/0/default.jpg' | ||
body.format = "image/jpeg" | ||
else: | ||
service = ServiceItem(id=infoJson['id'], profile=infoJson['profile'], type=infoJson['type']) | ||
body.service = [service] | ||
body.id = f'{infoJson["id"]}/full/max/0/default.jpg' | ||
body.format = "image/jpeg" | ||
|
||
annotation = Annotation(motivation='painting', body=body, target=canvas.id) | ||
|
||
annotationPage = AnnotationPage() | ||
annotationPage.add_item(annotation) | ||
|
||
canvas.add_item(annotationPage) | ||
canvas.set_hwd(infoJson['height'], infoJson['width']) | ||
|
||
return canvas | ||
|
||
def make_canvas_from_iiif(self, url, **kwargs): | ||
canvas = self.create_canvas_from_IIIF(url, **kwargs) | ||
|
||
self.add_item(canvas) | ||
|
||
|
||
monkeypatch_schema(Manifest, [CreateCanvasFromIIIF]) |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import unittest | ||
from unittest.mock import Mock, patch | ||
|
||
from iiif_prezi3 import Manifest | ||
|
||
|
||
class CreateCanvasFromIIIFTests(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.manifest = Manifest(label={'en': ['Manifest label']}) | ||
|
||
@patch('iiif_prezi3.helpers.set_hwd_from_iiif.requests.get') | ||
def test_create_canvas_from_iiif_v3(self, mockrequest_get): | ||
image_id = 'https://iiif.io/api/image/3.0/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/3/context.json", | ||
"extraFormats": ["jpg", "png"], | ||
"extraQualities": ["default", "color", "gray"], | ||
"height": 3024, | ||
"id": "https://iiif.io/api/image/3.0/example/reference/918ecd18c2592080851777620de9bcb5-gottingen", | ||
"profile": "level1", | ||
"protocol": "http://iiif.io/api/image", | ||
"tiles": [{ | ||
"height": 512, | ||
"scaleFactors": [1, 2, 4], | ||
"width": 512 | ||
}], | ||
"type": "ImageService3", | ||
"width": 4032 | ||
} | ||
|
||
canvas = self.manifest.create_canvas_from_iiif(image_info_url, label={'en': ['Canvas label']}) | ||
|
||
# Check canvas params made it through | ||
self.assertEqual(canvas.label, | ||
{'en': ['Canvas label']}) | ||
|
||
# check canvas dimensions | ||
self.assertEqual(canvas.height, 3024) | ||
self.assertEqual(canvas.width, 4032) | ||
|
||
# Check annotation | ||
annotation = canvas.items[0].items[0] | ||
self.assertEqual(annotation.motivation, "painting") | ||
|
||
# Check resource | ||
resource = annotation.body | ||
|
||
self.assertEqual(resource.id, f'{image_id}/full/max/0/default.jpg') | ||
self.assertEqual(resource.type, 'Image') | ||
self.assertEqual(resource.format, 'image/jpeg') | ||
self.assertEqual(resource.height, 3024) | ||
self.assertEqual(resource.width, 4032) | ||
|
||
# Check service | ||
service = resource.service[0] | ||
|
||
self.assertEqual(service.id, image_id) | ||
self.assertEqual(service.profile, "level1") | ||
self.assertEqual(service.type, "ImageService3") | ||
|
||
@patch('iiif_prezi3.helpers.set_hwd_from_iiif.requests.get') | ||
def test_create_canvas_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 | ||
} | ||
|
||
canvas = self.manifest.create_canvas_from_iiif(image_info_url, label={'en': ['Canvas label']}) | ||
|
||
# Check canvas params made it through | ||
self.assertEqual(canvas.label, | ||
{'en': ['Canvas label']}) | ||
|
||
# check canvas dimensions | ||
self.assertEqual(canvas.height, 3024) | ||
self.assertEqual(canvas.width, 4032) | ||
|
||
# Check annotation | ||
annotation = canvas.items[0].items[0] | ||
self.assertEqual(annotation.motivation, "painting") | ||
|
||
# Check resource | ||
resource = annotation.body | ||
|
||
self.assertEqual(resource.id, f'{image_id}/full/full/0/default.jpg') | ||
self.assertEqual(resource.type, 'Image') | ||
self.assertEqual(resource.format, 'image/jpeg') | ||
self.assertEqual(resource.height, 3024) | ||
self.assertEqual(resource.width, 4032) | ||
|
||
# Check service | ||
service = resource.service[0] | ||
|
||
self.assertEqual(service.id, image_id) | ||
self.assertEqual(service.profile, "http://iiif.io/api/image/2/level1.json") | ||
self.assertEqual(service.type, "ImageService2") | ||
|