From 992fb0bc915e689fc76fa6b021e364c3f0f17ca3 Mon Sep 17 00:00:00 2001 From: Benjamin Kiessling Date: Thu, 21 Dec 2023 09:24:19 +0100 Subject: [PATCH] Resolve circular imports --- kraken/lib/functional_im_transforms.py | 26 +++++++++++++------------- kraken/lib/lineest.py | 5 +++-- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/kraken/lib/functional_im_transforms.py b/kraken/lib/functional_im_transforms.py index 804428cc2..014389617 100644 --- a/kraken/lib/functional_im_transforms.py +++ b/kraken/lib/functional_im_transforms.py @@ -24,9 +24,8 @@ from pathlib import Path from PIL.Image import Resampling -from typing import Tuple, Optional, Callable, Any, Union, TYPE_CHECKING +from typing import Literal, Tuple, Optional, Callable, Any, Union, TYPE_CHECKING -from kraken.binarization import nlbin from kraken.lib.lineest import dewarp, CenterNormalizer if TYPE_CHECKING: @@ -34,11 +33,12 @@ from PIL import Image -def pil_to_mode(im: Image.Image, mode: str) -> Image.Image: +def pil_to_mode(im: 'Image.Image', mode: str) -> 'Image.Image': return im.convert(mode) -def pil_to_bin(im: Image.Image) -> Image.Image: +def pil_to_bin(im: 'Image.Image') -> 'Image.Image': + from kraken.binarization import nlbin return nlbin(im) @@ -46,11 +46,11 @@ def dummy(x: Any) -> Any: return x -def pil_dewarp(im: Image.Image, lnorm: CenterNormalizer) -> Image.Image: +def pil_dewarp(im: 'Image.Image', lnorm: CenterNormalizer) -> 'Image.Image': return dewarp(lnorm, im) -def pil_fixed_resize(im: Image.Image, scale: Tuple[int, int]) -> Image.Image: +def pil_fixed_resize(im: 'Image.Image', scale: Tuple[int, int]) -> 'Image.Image': return _fixed_resize(im, scale, Resampling.LANCZOS) @@ -62,14 +62,14 @@ def tensor_permute(im: torch.Tensor, perm: Tuple[int, ...]) -> torch.Tensor: return im.permute(*perm) -def _fixed_resize(img: Image.Image, size: Tuple[int, int], interpolation: int = Resampling.LANCZOS): +def _fixed_resize(img: 'Image.Image', size: Tuple[int, int], interpolation: int = Resampling.LANCZOS): """ Doesn't do the annoying runtime scale dimension switching the default pytorch transform does. Args: - img (PIL.Image.Image): image to resize - size (tuple): Tuple (height, width) + img: image to resize + size: Tuple (height, width) """ w, h = img.size oh, ow = size @@ -81,7 +81,7 @@ def _fixed_resize(img: Image.Image, size: Tuple[int, int], interpolation: int = return img -def text_normalize(text: str, normalization: str) -> str: +def text_normalize(text: str, normalization: Literal['NFD', 'NFC', 'NFKD', 'NFKC']) -> str: return unicodedata.normalize(normalization, text) @@ -89,16 +89,16 @@ def text_whitespace_normalize(text: str) -> str: return regex.sub(r'\s', ' ', text).strip() -def text_reorder(text: str, base_dir: Optional[str] = None) -> str: +def text_reorder(text: str, base_dir: Optional[Literal['L', 'R']] = None) -> str: return bd.get_display(text, base_dir=base_dir) -def default_split(x: Union[PathLike, str]) -> str: +def default_split(x: Union['PathLike', str]) -> str: x = Path(x) while x.suffixes: x = x.with_suffix('') return str(x) -def suffix_split(x: Union[PathLike, str], split: Callable[[Union[PathLike, str]], str], suffix: str) -> str: +def suffix_split(x: Union['PathLike', str], split: Callable[[Union['PathLike', str]], str], suffix: str) -> str: return split(x) + suffix diff --git a/kraken/lib/lineest.py b/kraken/lib/lineest.py index d7c5f3024..948559bdb 100644 --- a/kraken/lib/lineest.py +++ b/kraken/lib/lineest.py @@ -1,7 +1,6 @@ import warnings import numpy as np -from kraken.lib.util import pil2array, array2pil from scipy.ndimage import affine_transform, gaussian_filter, uniform_filter from typing import TYPE_CHECKING @@ -77,8 +76,10 @@ def dewarp(normalizer: CenterNormalizer, im: 'Image.Image') -> 'Image.Image': im: Image to dewarp Returns: - PIL.Image containing the dewarped image. + PIL.Image.Image containing the dewarped image. """ + from kraken.lib.util import pil2array, array2pil + line = pil2array(im) temp = np.amax(line)-line temp = temp*1.0/np.amax(temp)