Skip to content

Pil

PILLoader

Bases: KoshLoader

Source code in kosh/loaders/pil.py
class PILLoader(KoshLoader):
    types = {"png": ["numpy", "bytes"],
             "gif": ["numpy", "bytes"],
             "image": ["numpy", "bytes"],
             "tiff": ["numpy", "bytes"],
             "pil": ["numpy", "bytes"],
             "tif": ["numpy", "bytes"]}

    def __init__(self, obj, **kargs):
        """ImageLoader for Kosh to be able to read in pillow (PIL) compatible image files

        :param obj: Kosh obj reference
        :type obj: object
        """
        super(PILLoader, self).__init__(obj, **kargs)

    def open(self, mode="r"):
        """open the pil reader

        :param mode: mode to open the file in, defaults to 'r'
        :type mode: str, optional
        :return: Image file from PIL
        """
        from PIL import Image
        return Image.open(self.uri)

    def extract(self):
        """get a feature

        :param feature: in this case element/metric
        :type feature: str
        :param format: desired output format (numpy only for now)
        :type format: str
        :return: numpy array or raw bytes
        :rtype: numpy.ndarray or bytes
        """
        if self.format == "numpy":
            return numpy.array(self.open())
        elif self.format == "bytes":
            obj = self.open()
            raw = obj.tobytes()
            return raw

    def list_features(self):
        """list_features lists features available

        :return: list of features you can retrieve ["image", ] in our case
        :rtype: list
        """

        return ["image", ]

    def describe_feature(self, feature):
        """describe_feature describe the feature as a dictionary

        :param feature: feature to describe
        :type feature: str
        :return: dictionary with attributes describing the feature: size, mode, format
        :rtype: dict
        """
        image = self.open()
        return {"size": image.size, "mode": image.mode, "format": image.format}

__init__(obj, **kargs)

ImageLoader for Kosh to be able to read in pillow (PIL) compatible image files

Parameters:

Name Type Description Default
obj object

Kosh obj reference

required
Source code in kosh/loaders/pil.py
def __init__(self, obj, **kargs):
    """ImageLoader for Kosh to be able to read in pillow (PIL) compatible image files

    :param obj: Kosh obj reference
    :type obj: object
    """
    super(PILLoader, self).__init__(obj, **kargs)

describe_feature(feature)

describe_feature describe the feature as a dictionary

Parameters:

Name Type Description Default
feature str

feature to describe

required

Returns:

Type Description
dict

dictionary with attributes describing the feature: size, mode, format

Source code in kosh/loaders/pil.py
def describe_feature(self, feature):
    """describe_feature describe the feature as a dictionary

    :param feature: feature to describe
    :type feature: str
    :return: dictionary with attributes describing the feature: size, mode, format
    :rtype: dict
    """
    image = self.open()
    return {"size": image.size, "mode": image.mode, "format": image.format}

extract()

get a feature

Parameters:

Name Type Description Default
feature str

in this case element/metric

required
format str

desired output format (numpy only for now)

required

Returns:

Type Description
numpy.ndarray | bytes

numpy array or raw bytes

Source code in kosh/loaders/pil.py
def extract(self):
    """get a feature

    :param feature: in this case element/metric
    :type feature: str
    :param format: desired output format (numpy only for now)
    :type format: str
    :return: numpy array or raw bytes
    :rtype: numpy.ndarray or bytes
    """
    if self.format == "numpy":
        return numpy.array(self.open())
    elif self.format == "bytes":
        obj = self.open()
        raw = obj.tobytes()
        return raw

list_features()

list_features lists features available

Returns:

Type Description
list

list of features you can retrieve ["image", ] in our case

Source code in kosh/loaders/pil.py
def list_features(self):
    """list_features lists features available

    :return: list of features you can retrieve ["image", ] in our case
    :rtype: list
    """

    return ["image", ]

open(mode='r')

open the pil reader

Parameters:

Name Type Description Default
mode str, optional

mode to open the file in, defaults to 'r'

'r'

Returns:

Type Description

Image file from PIL

Source code in kosh/loaders/pil.py
def open(self, mode="r"):
    """open the pil reader

    :param mode: mode to open the file in, defaults to 'r'
    :type mode: str, optional
    :return: Image file from PIL
    """
    from PIL import Image
    return Image.open(self.uri)