From 8103ab92b9d8f1898164ded197d7ec60671e2aca Mon Sep 17 00:00:00 2001 From: Gustaf Rydholm Date: Sun, 6 Feb 2022 22:46:01 +0100 Subject: fix: refactor and move image utils --- text_recognizer/data/utils/image_utils.py | 49 ------------------------------- 1 file changed, 49 deletions(-) delete mode 100644 text_recognizer/data/utils/image_utils.py (limited to 'text_recognizer/data/utils') diff --git a/text_recognizer/data/utils/image_utils.py b/text_recognizer/data/utils/image_utils.py deleted file mode 100644 index c2b8915..0000000 --- a/text_recognizer/data/utils/image_utils.py +++ /dev/null @@ -1,49 +0,0 @@ -"""Image util functions for loading and saving images.""" -from pathlib import Path -from typing import Union -from urllib.request import urlopen - -import cv2 -import numpy as np -from PIL import Image - - -def read_image_pil(image_uri: Union[Path, str], grayscale: bool = False) -> Image: - """Return PIL image.""" - image = Image.open(image_uri) - if grayscale: - image = image.convert("L") - return image - - -def read_image(image_uri: Union[Path, str], grayscale: bool = False) -> np.array: - """Read image_uri.""" - - if isinstance(image_uri, str): - image_uri = Path(image_uri) - - def read_image_from_filename(image_filename: Path, imread_flag: int) -> np.array: - return cv2.imread(str(image_filename), imread_flag) - - def read_image_from_url(image_url: Path, imread_flag: int) -> np.array: - url_response = urlopen(str(image_url)) # nosec - image_array = np.array(bytearray(url_response.read()), dtype=np.uint8) - return cv2.imdecode(image_array, imread_flag) - - imread_flag = cv2.IMREAD_GRAYSCALE if grayscale else cv2.IMREAD_COLOR - image = None - - if image_uri.exists(): - image = read_image_from_filename(image_uri, imread_flag) - else: - image = read_image_from_url(image_uri, imread_flag) - - if image is None: - raise ValueError(f"Could not load image at {image_uri}") - - return image - - -def write_image(image: np.ndarray, filename: Union[Path, str]) -> None: - """Write image to file.""" - cv2.imwrite(str(filename), image) -- cgit v1.2.3-70-g09d2