Skip to content
Snippets Groups Projects

Implement UFCN custom_open_image behaviour in basic open_image function

Merged Eva Bardou requested to merge custom-open-image into master
All threads resolved!
2 files
+ 34
17
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 20
3
@@ -55,6 +55,14 @@ class Element(MagicDict):
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"):
"""
When possible, will return the S3 URL for images, so an ML worker can bypass IIIF servers
@@ -79,10 +87,16 @@ class Element(MagicDict):
bounding_box = polygon_bounding_box(self.zone.polygon)
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.
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.
"""
if not self.get("zone"):
@@ -119,7 +133,10 @@ class Element(MagicDict):
resize = "full"
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:
if (
self.zone.image.get("s3_url") is not None
Loading