33import os
44from typing import List
55
6+ from PIL import Image , UnidentifiedImageError
7+
68from kili .core .constants import mime_extensions_that_need_post_processing
79from kili .core .helpers import get_mime_type
810from kili .domain .project import InputType
911
1012from .base import BaseAbstractAssetImporter , BatchParams , ContentBatchImporter
11- from .constants import LARGE_IMAGE_THRESHOLD_SIZE
13+ from .constants import LARGE_IMAGE_THRESHOLD_SIZE , MAX_WIDTH_OR_HEIGHT_NON_TILED
1214from .types import AssetLike
1315
16+ Image .MAX_IMAGE_PIXELS = None
17+
1418
1519class ImageDataImporter (BaseAbstractAssetImporter ):
1620 """Class for importing assets into an IMAGE or GEOSPATIAL project."""
@@ -46,6 +50,16 @@ def import_assets(self, assets: List[AssetLike], input_type: InputType):
4650 )
4751 return created_asset_ids
4852
53+ @staticmethod
54+ def get_is_large_image (image_path : str ) -> bool :
55+ """Define if an image is too large and so on has to be tiled."""
56+ if os .path .getsize (image_path ) >= LARGE_IMAGE_THRESHOLD_SIZE :
57+ return True
58+
59+ image = Image .open (image_path )
60+ width , height = image .size
61+ return width >= MAX_WIDTH_OR_HEIGHT_NON_TILED or height >= MAX_WIDTH_OR_HEIGHT_NON_TILED
62+
4963 @staticmethod
5064 def split_asset_by_upload_type (assets : List [AssetLike ], is_hosted : bool ):
5165 """Split assets into two groups, assets to to imported synchronously or asynchronously."""
@@ -65,9 +79,15 @@ def split_asset_by_upload_type(assets: List[AssetLike], is_hosted: bool):
6579 assert path
6680 assert isinstance (path , str )
6781 mime_type = get_mime_type (path )
68- is_large_image = os .path .getsize (path ) >= LARGE_IMAGE_THRESHOLD_SIZE
69- if is_large_image or mime_type in mime_extensions_that_need_post_processing :
82+ if mime_type in mime_extensions_that_need_post_processing :
7083 async_assets .append (asset )
7184 else :
72- sync_assets .append (asset )
85+ try :
86+ is_large_image = ImageDataImporter .get_is_large_image (path )
87+ if is_large_image :
88+ async_assets .append (asset )
89+ else :
90+ sync_assets .append (asset )
91+ except UnidentifiedImageError :
92+ async_assets .append (asset )
7393 return sync_assets , async_assets
0 commit comments