From 49069253b8eba643aecbc1972879be9517801272 Mon Sep 17 00:00:00 2001 From: Gustaf Rydholm Date: Tue, 27 Sep 2022 23:19:10 +0200 Subject: Move utils --- text_recognizer/image_utils.py | 34 ---------------------------------- text_recognizer/util/__init__.py | 0 text_recognizer/util/image_utils.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 34 deletions(-) delete mode 100644 text_recognizer/image_utils.py create mode 100644 text_recognizer/util/__init__.py create mode 100644 text_recognizer/util/image_utils.py diff --git a/text_recognizer/image_utils.py b/text_recognizer/image_utils.py deleted file mode 100644 index fcb094c..0000000 --- a/text_recognizer/image_utils.py +++ /dev/null @@ -1,34 +0,0 @@ -"""Image util functions for loading and saving images.""" -import base64 -from io import BytesIO -from pathlib import Path -from typing import Any, Union - -import smart_open -from PIL import Image - - -def read_image_pil(image_uri: Union[Path, str], grayscale: bool = False) -> Image: - """Read image from uri.""" - with smart_open(image_uri, "rb") as image_file: - return read_image_pil_file(image_file, grayscale) - - -def read_image_pil_file(image_file: str, grayscale: bool = False) -> Image: - """Return PIL image.""" - with Image.open(image_file) as image: - if grayscale: - image = image.convert("L") - else: - image = image.convert(mode=image.mode) - return image - - -def read_b64_image(b64_str: str, grayscale: bool = False) -> Image: - """Load base64-encoded images.""" - try: - _, b64_data = b64_str.split(",") - image_file = BytesIO(base64.b64decode(b64_data)) - return read_image_pil_file(image_file, grayscale) - except Exception as e: - raise ValueError(f"Could not load image from b64 {b64_str}: {e}") diff --git a/text_recognizer/util/__init__.py b/text_recognizer/util/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/text_recognizer/util/image_utils.py b/text_recognizer/util/image_utils.py new file mode 100644 index 0000000..fcb094c --- /dev/null +++ b/text_recognizer/util/image_utils.py @@ -0,0 +1,34 @@ +"""Image util functions for loading and saving images.""" +import base64 +from io import BytesIO +from pathlib import Path +from typing import Any, Union + +import smart_open +from PIL import Image + + +def read_image_pil(image_uri: Union[Path, str], grayscale: bool = False) -> Image: + """Read image from uri.""" + with smart_open(image_uri, "rb") as image_file: + return read_image_pil_file(image_file, grayscale) + + +def read_image_pil_file(image_file: str, grayscale: bool = False) -> Image: + """Return PIL image.""" + with Image.open(image_file) as image: + if grayscale: + image = image.convert("L") + else: + image = image.convert(mode=image.mode) + return image + + +def read_b64_image(b64_str: str, grayscale: bool = False) -> Image: + """Load base64-encoded images.""" + try: + _, b64_data = b64_str.split(",") + image_file = BytesIO(base64.b64decode(b64_data)) + return read_image_pil_file(image_file, grayscale) + except Exception as e: + raise ValueError(f"Could not load image from b64 {b64_str}: {e}") -- cgit v1.2.3-70-g09d2