caikit.interfaces.vision.data_model.backends.image_pil_backend

Defines the backend for making Image data model objects work nicely with PIL images. The ImagePilBackend outlines the manner in which we coerce our inputs to PIL images, retrieve/cache data model attributes from the encapsulated PIL image, and so on.

Attributes

log

error

PIL_SOURCE_TYPES

Classes

ImagePilBackend

A base interface class for accessing data from within a given backend

Module Contents

caikit.interfaces.vision.data_model.backends.image_pil_backend.log[source]
caikit.interfaces.vision.data_model.backends.image_pil_backend.error
caikit.interfaces.vision.data_model.backends.image_pil_backend.PIL_SOURCE_TYPES
class caikit.interfaces.vision.data_model.backends.image_pil_backend.ImagePilBackend(image_data: PIL_SOURCE_TYPES)[source]

Bases: caikit.core.data_model.data_backends.DataModelBackendBase

A base interface class for accessing data from within a given backend data layout

_image_data
_export_format
get_attribute(data_model_class: caikit.core.data_model.base.DataBase, name: str) Any[source]

A data model backend must implement this in order to provide the frontend view the functionality needed to lazily extract data.

Args:
data_model_class (Type[DataBase]): The frontend data model class

that is accessing this attribute

name (str): The name of the attribute to access

Returns:
value: Union[Any, OneofFieldVal]

The extracted attribute value or a OneofFieldVal that wraps the field val with an indicator about the oneof field that is set.

classmethod coerce_to_pil(image_data: PIL_SOURCE_TYPES) PIL.Image[source]

Given an object representing an image in a wide variety of formats, force it into a PIL image representation. Supported formats:

  • [PIL.Image.Image] loaded PIL image; this is a no-op

  • [str] path to an image on disk to be loaded

  • [pathlib.PosixPath] path to an image on disk to be loaded

  • [np.ndarray] Numpy array of type uint8

  • [bytes] Image loaded into a bytes object

Args:
image_data:

Raw data object to be coerced to a PIL image.

Returns:
PILImage.Image

PIL image representation of the provided image data.

static _coerce_from_numpy(image_data: numpy.ndarray) PIL.Image.Image[source]

Given image data as a Numpy array, load it as a PIL image.

Args:
image_data: np.ndarray

Numpy array representing an image (uint8).

Returns:
PIL.Image.Image

PIL image representation of the numpy array.

classmethod _coerce_from_path(image_data: str | pathlib.PosixPath) PIL.Image[source]

Given a str, which we assume to be a path to an image on disk, or a Pathlib object, try to load it as a PIL image.

Args:
image_data: Union[pathlib.PosixPath, str]

Path to be loaded.

Returns:
PIL.Image.Image

PIL image representation of the image loaded from disk.

static _coerce_from_bytes(image_data: bytes) PIL.Image[source]

Given bytes, which we assume to represent a full image, try to load it as a PIL image. Args:

image_data: bytes

binary data to be loaded as a PIL image.

Returns:
PIL.Image.Image

PIL image representation of the bytes object.

as_numpy() numpy.ndarray[source]

Zero-copy method to produce the PIL image as a numpy array. Returns:

np.ndarray

Numpy array representation of the PIL image data.

as_pil() PIL.Image.Image[source]

Return a handle to our PIL image. Returns:

PIL.Image.Image

PIL image representation of this data model object.