ImageLoader

Component that can turn filepaths into a list of PIL.Image objects.

Parameters

Name Type Description Default
convert str Color conversion setting from the Python image library. 'RGB'
out str What kind of image output format to expect. 'pil'

Usage

You can use the ImageLoader in standalone fashion.

from embetter.vision import ImageLoader

filepath = "tests/data/thiscatdoesnotexist.jpeg"
ImageLoader(convert="RGB").fit_transform([filepath])

But it's more common to see it part of a pipeline.

import pandas as pd
from sklearn.pipeline import make_pipeline

from embetter.grab import ColumnGrabber
from embetter.vision import ImageLoader, ColorHistogramEncoder

# Let's say we start we start with a csv file with filepaths
data = {"filepaths":  ["tests/data/thiscatdoesnotexist.jpeg"]}
df = pd.DataFrame(data)

# Let's build a pipeline that grabs the column, turns it
# into an image and embeds it.
pipe = make_pipeline(
    ColumnGrabber("filepaths"),
    ImageLoader(),
    ColorHistogramEncoder()
)

pipe.fit_transform(df)

fit(self, X, y=None)

Show source code in vision/_loader.py
57
58
59
60
61
62
63
64
65
66
    def fit(self, X, y=None):
        """
        Not actual "fitting" happens in this method, but it does check the input arguments
        per sklearn convention.
        """
        if self.out not in ["pil", "numpy"]:
            raise ValueError(
                f"Output format parameter out={self.out} must be either pil/numpy."
            )
        return self

Not actual "fitting" happens in this method, but it does check the input arguments per sklearn convention.

transform(self, X, y=None)

Show source code in vision/_loader.py
68
69
70
71
72
73
74
75
    def transform(self, X, y=None):
        """
        Turn a file path into numpy array containing pixel values.
        """
        if self.out == "pil":
            return [Image.open(x).convert(self.convert) for x in X]
        if self.out == "numpy":
            return np.array([np.array(Image.open(x).convert(self.convert)) for x in X])

Turn a file path into numpy array containing pixel values.