-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcreate_canvas_from_iiif.py
68 lines (48 loc) · 2.39 KB
/
create_canvas_from_iiif.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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, anno_id=None, anno_page_id=None, **kwargs):
"""Create a canvas from a IIIF Image URL.
Creates a canvas from a IIIF Image service passing any kwargs to the Canvas.
Args:
url (str): An HTTP URL at which at a IIIF Image is available.
anno_id (str): An HTTP URL for the annotation to which the image will be attached.
anno_page_id (str): An HTTP URL for the annotation page to which the annotation will be attached.
**kwargs (): see Canvas
Returns:
canvas (Canvas): the Canvas created from the IIIF Image.
"""
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(id=anno_id, motivation='painting', body=body, target=canvas.id)
annotationPage = AnnotationPage(id=anno_page_id)
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)
return canvas
monkeypatch_schema(Manifest, [CreateCanvasFromIIIF])