Skip to content

Commit

Permalink
Resolve circular imports
Browse files Browse the repository at this point in the history
  • Loading branch information
mittagessen committed Dec 21, 2023
1 parent 283a4e3 commit 992fb0b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
26 changes: 13 additions & 13 deletions kraken/lib/functional_im_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,33 @@
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:
from os import PathLike
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)


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)


Expand All @@ -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
Expand All @@ -81,24 +81,24 @@ 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)


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
5 changes: 3 additions & 2 deletions kraken/lib/lineest.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 992fb0b

Please sign in to comment.