Skip to content
Snippets Groups Projects
Commit e25de69d authored by Yoann Schneider's avatar Yoann Schneider :tennis: Committed by Solene Tarride
Browse files

Fix resizing

parent e07f5385
No related branches found
No related tags found
1 merge request!279Fix resizing
......@@ -6,7 +6,6 @@ import pickle
import random
from collections import defaultdict
from concurrent.futures import Future, ThreadPoolExecutor
from functools import cached_property
from pathlib import Path
from typing import Dict, List, Optional, Union
from uuid import UUID
......@@ -91,23 +90,26 @@ class ArkindexExtractor:
# Image download tasks to process
self.tasks: List[Dict[str, str]] = []
@cached_property
def max_resize(self):
# We keep the aspect ratio so we only use on dimension, the biggest one
if self.max_width > self.max_height:
return f"{self.max_width},"
return f",{self.max_height}"
def get_iiif_size_arg(self, width: int, height: int) -> str:
if (self.max_width is None or width <= self.max_width) and (
self.max_height is None or height <= self.max_height
):
return IIIF_FULL_SIZE
# Resizing if the image is bigger than the wanted size.
if self.max_width and self.max_height:
return self.max_resize
return f"{self.max_width or ''},{self.max_height or ''}"
bigger_width = self.max_width and width >= self.max_width
bigger_height = self.max_height and height >= self.max_height
if bigger_width and bigger_height:
# Resize to the biggest dim to keep aspect ratio
# Only resize width is bigger than max size
# This ratio tells which dim needs the biggest shrinking
ratio = width * self.max_height / (height * self.max_width)
return f"{self.max_width}," if ratio > 1 else f",{self.max_height}"
elif bigger_width:
return f"{self.max_width},"
# Only resize height is bigger than max size
elif bigger_height:
return f",{self.max_height}"
def build_iiif_url(self, polygon, image_url):
bbox = polygon_to_bbox(json.loads(str(polygon)))
......
......@@ -12,7 +12,7 @@ import pytest
from PIL import Image, ImageChops
from dan.datasets.extract.exceptions import NoEndTokenError
from dan.datasets.extract.extract import ArkindexExtractor
from dan.datasets.extract.extract import IIIF_FULL_SIZE, ArkindexExtractor
from dan.datasets.extract.utils import EntityType, insert_token, remove_spaces
from dan.utils import parse_tokens
from tests import FIXTURES
......@@ -36,6 +36,24 @@ def filter_tokens(keys):
return {key: value for key, value in TOKENS.items() if key in keys}
@pytest.mark.parametrize(
"max_width, max_height, width, height, resize",
(
(1000, 2000, 900, 800, IIIF_FULL_SIZE),
(1000, 2000, 1100, 800, "1000,"),
(1000, 2000, 1100, 2800, ",2000"),
(1000, 2000, 2000, 3000, "1000,"),
),
)
def test_get_iiif_size_arg(max_width, max_height, width, height, resize):
assert (
ArkindexExtractor(max_width=max_width, max_height=max_height).get_iiif_size_arg(
width=width, height=height
)
== resize
)
@pytest.mark.parametrize(
"text,offset,length,expected",
(
......
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