Skip to content
Snippets Groups Projects
Commit 06958b3c authored by Eva Bardou's avatar Eva Bardou Committed by Bastien Abadie
Browse files

Implement UFCN custom_open_image behaviour in basic open_image function

parent 92e66b78
No related branches found
No related tags found
1 merge request!102Implement UFCN custom_open_image behaviour in basic open_image function
Pipeline #78515 passed
...@@ -55,6 +55,14 @@ class Element(MagicDict): ...@@ -55,6 +55,14 @@ class Element(MagicDict):
Describes any kind of element. Describes any kind of element.
""" """
def resize_zone_url(self, size="full"):
if size == "full":
return self.zone.url
else:
parts = self.zone.url.split("/")
parts[-3] = size
return "/".join(parts)
def image_url(self, size="full"): def image_url(self, size="full"):
""" """
When possible, will return the S3 URL for images, so an ML worker can bypass IIIF servers When possible, will return the S3 URL for images, so an ML worker can bypass IIIF servers
...@@ -79,10 +87,16 @@ class Element(MagicDict): ...@@ -79,10 +87,16 @@ class Element(MagicDict):
bounding_box = polygon_bounding_box(self.zone.polygon) bounding_box = polygon_bounding_box(self.zone.polygon)
return bounding_box.width > max_width or bounding_box.height > max_height return bounding_box.width > max_width or bounding_box.height > max_height
def open_image(self, *args, max_size=None, **kwargs): def open_image(self, *args, max_size=None, use_full_image=False, **kwargs):
""" """
Open this element's image as a Pillow image. Open this element's image as a Pillow image.
This does not crop the image to the element's polygon.
If use_full_image is False:
Use zone url, instead of the full image url to be able to get
single page from double page image.
Else:
This does not crop the image to the element's polygon.
:param max_size: Subresolution of the image. :param max_size: Subresolution of the image.
""" """
if not self.get("zone"): if not self.get("zone"):
...@@ -119,7 +133,10 @@ class Element(MagicDict): ...@@ -119,7 +133,10 @@ class Element(MagicDict):
resize = "full" resize = "full"
try: try:
return open_image(self.image_url(resize), *args, **kwargs) if use_full_image:
return open_image(self.image_url(resize), *args, **kwargs)
else:
return open_image(self.resize_zone_url(resize), *args, **kwargs)
except HTTPError as e: except HTTPError as e:
if ( if (
self.zone.image.get("s3_url") is not None self.zone.image.get("s3_url") is not None
......
...@@ -62,7 +62,7 @@ def test_open_image(mocker): ...@@ -62,7 +62,7 @@ def test_open_image(mocker):
} }
} }
) )
assert elt.open_image() == "an image!" assert elt.open_image(use_full_image=True) == "an image!"
assert open_mock.call_count == 1 assert open_mock.call_count == 1
assert open_mock.call_args == mocker.call( assert open_mock.call_args == mocker.call(
"http://something/full/full/0/default.jpg" "http://something/full/full/0/default.jpg"
...@@ -87,19 +87,19 @@ def test_open_image_resize_portrait(mocker): ...@@ -87,19 +87,19 @@ def test_open_image_resize_portrait(mocker):
} }
) )
# Resize = original size # Resize = original size
assert elt.open_image(max_size=600) == "an image!" assert elt.open_image(max_size=600, use_full_image=True) == "an image!"
assert open_mock.call_count == 1 assert open_mock.call_count == 1
assert open_mock.call_args == mocker.call( assert open_mock.call_args == mocker.call(
"http://something/full/full/0/default.jpg" "http://something/full/full/0/default.jpg"
) )
# Resize = smaller height # Resize = smaller height
assert elt.open_image(max_size=400) == "an image!" assert elt.open_image(max_size=400, use_full_image=True) == "an image!"
assert open_mock.call_count == 2 assert open_mock.call_count == 2
assert open_mock.call_args == mocker.call( assert open_mock.call_args == mocker.call(
"http://something/full/266,400/0/default.jpg" "http://something/full/266,400/0/default.jpg"
) )
# Resize = bigger height # Resize = bigger height
assert elt.open_image(max_size=800) == "an image!" assert elt.open_image(max_size=800, use_full_image=True) == "an image!"
assert open_mock.call_count == 3 assert open_mock.call_count == 3
assert open_mock.call_args == mocker.call( assert open_mock.call_args == mocker.call(
"http://something/full/full/0/default.jpg" "http://something/full/full/0/default.jpg"
...@@ -123,7 +123,7 @@ def test_open_image_resize_partial_element(mocker): ...@@ -123,7 +123,7 @@ def test_open_image_resize_partial_element(mocker):
} }
} }
) )
assert elt.open_image(max_size=400) == "an image!" assert elt.open_image(max_size=400, use_full_image=True) == "an image!"
assert open_mock.call_count == 1 assert open_mock.call_count == 1
assert open_mock.call_args == mocker.call( assert open_mock.call_args == mocker.call(
"http://something/full/full/0/default.jpg" "http://something/full/full/0/default.jpg"
...@@ -148,19 +148,19 @@ def test_open_image_resize_landscape(mocker): ...@@ -148,19 +148,19 @@ def test_open_image_resize_landscape(mocker):
} }
) )
# Resize = original size # Resize = original size
assert elt.open_image(max_size=600) == "an image!" assert elt.open_image(max_size=600, use_full_image=True) == "an image!"
assert open_mock.call_count == 1 assert open_mock.call_count == 1
assert open_mock.call_args == mocker.call( assert open_mock.call_args == mocker.call(
"http://something/full/full/0/default.jpg" "http://something/full/full/0/default.jpg"
) )
# Resize = smaller width # Resize = smaller width
assert elt.open_image(max_size=400) == "an image!" assert elt.open_image(max_size=400, use_full_image=True) == "an image!"
assert open_mock.call_count == 2 assert open_mock.call_count == 2
assert open_mock.call_args == mocker.call( assert open_mock.call_args == mocker.call(
"http://something/full/400,266/0/default.jpg" "http://something/full/400,266/0/default.jpg"
) )
# Resize = bigger width # Resize = bigger width
assert elt.open_image(max_size=800) == "an image!" assert elt.open_image(max_size=800, use_full_image=True) == "an image!"
assert open_mock.call_count == 3 assert open_mock.call_count == 3
assert open_mock.call_args == mocker.call( assert open_mock.call_args == mocker.call(
"http://something/full/full/0/default.jpg" "http://something/full/full/0/default.jpg"
...@@ -185,19 +185,19 @@ def test_open_image_resize_square(mocker): ...@@ -185,19 +185,19 @@ def test_open_image_resize_square(mocker):
} }
) )
# Resize = original size # Resize = original size
assert elt.open_image(max_size=400) == "an image!" assert elt.open_image(max_size=400, use_full_image=True) == "an image!"
assert open_mock.call_count == 1 assert open_mock.call_count == 1
assert open_mock.call_args == mocker.call( assert open_mock.call_args == mocker.call(
"http://something/full/full/0/default.jpg" "http://something/full/full/0/default.jpg"
) )
# Resize = smaller # Resize = smaller
assert elt.open_image(max_size=200) == "an image!" assert elt.open_image(max_size=200, use_full_image=True) == "an image!"
assert open_mock.call_count == 2 assert open_mock.call_count == 2
assert open_mock.call_args == mocker.call( assert open_mock.call_args == mocker.call(
"http://something/full/200,200/0/default.jpg" "http://something/full/200,200/0/default.jpg"
) )
# Resize = bigger # Resize = bigger
assert elt.open_image(max_size=800) == "an image!" assert elt.open_image(max_size=800, use_full_image=True) == "an image!"
assert open_mock.call_count == 3 assert open_mock.call_count == 3
assert open_mock.call_args == mocker.call( assert open_mock.call_args == mocker.call(
"http://something/full/full/0/default.jpg" "http://something/full/full/0/default.jpg"
...@@ -234,7 +234,7 @@ def test_open_image_s3(mocker): ...@@ -234,7 +234,7 @@ def test_open_image_s3(mocker):
elt = Element( elt = Element(
{"zone": {"image": {"url": "http://something", "s3_url": "http://s3url"}}} {"zone": {"image": {"url": "http://something", "s3_url": "http://s3url"}}}
) )
assert elt.open_image() == "an image!" assert elt.open_image(use_full_image=True) == "an image!"
assert open_mock.call_count == 1 assert open_mock.call_count == 1
assert open_mock.call_args == mocker.call("http://s3url") assert open_mock.call_args == mocker.call("http://s3url")
...@@ -256,7 +256,7 @@ def test_open_image_s3_retry(mocker): ...@@ -256,7 +256,7 @@ def test_open_image_s3_retry(mocker):
) )
with pytest.raises(NotImplementedError): with pytest.raises(NotImplementedError):
elt.open_image() elt.open_image(use_full_image=True)
def test_open_image_s3_retry_once(mocker): def test_open_image_s3_retry_once(mocker):
...@@ -274,4 +274,49 @@ def test_open_image_s3_retry_once(mocker): ...@@ -274,4 +274,49 @@ def test_open_image_s3_retry_once(mocker):
) )
with pytest.raises(NotImplementedError): with pytest.raises(NotImplementedError):
elt.open_image() elt.open_image(use_full_image=True)
def test_open_image_use_full_image_false(mocker):
open_mock = mocker.patch(
"arkindex_worker.models.open_image", return_value="an image!"
)
elt = Element(
{
"zone": {
"image": {"url": "http://something", "s3_url": "http://s3url"},
"url": "http://zoneurl/0,0,400,600/full/0/default.jpg",
}
}
)
assert elt.open_image(use_full_image=False) == "an image!"
assert open_mock.call_count == 1
assert open_mock.call_args == mocker.call(
"http://zoneurl/0,0,400,600/full/0/default.jpg"
)
def test_open_image_resize_use_full_image_false(mocker):
open_mock = mocker.patch(
"arkindex_worker.models.open_image", return_value="an image!"
)
elt = Element(
{
"zone": {
"image": {
"url": "http://something",
"width": 400,
"height": 600,
"server": {"max_width": None, "max_height": None},
},
"polygon": [[0, 0], [400, 0], [400, 600], [0, 600], [0, 0]],
"url": "http://zoneurl/0,0,400,600/full/0/default.jpg",
}
}
)
# Resize = smaller
assert elt.open_image(max_size=200, use_full_image=False) == "an image!"
assert open_mock.call_count == 1
assert open_mock.call_args == mocker.call(
"http://zoneurl/0,0,400,600/133,200/0/default.jpg"
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment