Commit 67ef8bc8 authored by narugo1992's avatar narugo1992
Browse files

dev(narugo): add new features

parent d99c5185
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -2,8 +2,8 @@

[![PyPI](https://img.shields.io/pypi/v/dghs-imgutils)](https://pypi.org/project/dghs-imgutils/)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/dghs-imgutils)
![Loc](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/deepghs/8bfaa96eaa25cc9dac54debbf22d363d/raw/loc.json)
![Comments](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/deepghs/8bfaa96eaa25cc9dac54debbf22d363d/raw/comments.json)
![Loc](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/narugo1992/8bfaa96eaa25cc9dac54debbf22d363d/raw/loc.json)
![Comments](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/narugo1992/8bfaa96eaa25cc9dac54debbf22d363d/raw/comments.json)

[![Code Test](https://github.com/deepghs/imgutils/workflows/Code%20Test/badge.svg)](https://github.com/deepghs/imgutils/actions?query=workflow%3A%22Code+Test%22)
[![Package Release](https://github.com/deepghs/imgutils/workflows/Package%20Release/badge.svg)](https://github.com/deepghs/imgutils/actions?query=workflow%3A%22Package+Release%22)
+3 −0
Original line number Diff line number Diff line
from .decode import rgb_decode
from .encode import rgb_encode
from .image import ImageTyping, load_image
+23 −0
Original line number Diff line number Diff line
import numpy as np
from PIL import Image

_DEFAULT_ORDER = 'HWC'


def _get_hwc_map(order_: str):
    order_ = order_.upper()
    return tuple(order_.index(c) for c in _DEFAULT_ORDER.upper())


def rgb_decode(data, order_: str = 'CHW') -> Image.Image:
    if data.dtype in (np.uint8, np.int8, np.uint16, np.int16,
                      np.uint32, np.int32, np.uint64, np.int64):
        data = data.astype(np.uint8)
    elif data.dtype in (np.float16, np.float32, np.float64, np.float128):
        data = np.clip(data, 0.0, 1.0)
        data = (data * 255).astype(np.uint8)
    else:
        raise TypeError(f'Unknown dtype for data - {data.dtype!r}.')  # pragma: no cover

    data = np.transpose(data, _get_hwc_map(order_))
    return Image.fromarray(data, 'RGB')
+21 −0
Original line number Diff line number Diff line
import numpy as np

from .image import load_image, ImageTyping

_DEFAULT_ORDER = 'HWC'


def _get_hwc_map(order_: str):
    return tuple(_DEFAULT_ORDER.index(c) for c in order_.upper())


def rgb_encode(image: ImageTyping, order_: str = 'CHW', use_float: bool = True) -> np.ndarray:
    image = load_image(image, mode='RGB')
    array = np.asarray(image)
    array = np.transpose(array, _get_hwc_map(order_))
    if use_float:
        array = (array / 255.0).astype(np.float32)
        assert array.dtype == np.float32
    else:
        assert array.dtype == np.uint8
    return array

imgutils/data/image.py

0 → 100644
+25 −0
Original line number Diff line number Diff line
from os import PathLike
from typing import Union, BinaryIO

from PIL import Image


def _is_readable(obj):
    return hasattr(obj, 'read') and hasattr(obj, 'seek')


ImageTyping = Union[str, PathLike, bytes, bytearray, BinaryIO, Image.Image]


def load_image(image: ImageTyping, mode=None):
    if isinstance(image, (str, PathLike, bytes, bytearray, BinaryIO)) or _is_readable(image):
        image = Image.open(image)
    elif isinstance(image, Image.Image):
        pass  # just do nothing
    else:
        raise TypeError(f'Unknown image type - {image!r}.')

    if mode is not None and image.mode != mode:
        image = image.convert(mode)

    return image
Loading