Skip to content

Image loader

ImageLoader

Bases: BaseLoader

Loader for images.

Attributes:

Name Type Description
format str

Attempt to ensure image artifacts are in this format when loaded. For example, when set to 'PNG', loading image.jpg will return an ImageArtifact contianing the image bytes in PNG format. Defaults to PNG.

Source code in griptape/griptape/loaders/image_loader.py
@define
class ImageLoader(BaseLoader):
    """Loader for images.

    Attributes:
        format: Attempt to ensure image artifacts are in this format when loaded. For example, when set to 'PNG',
            loading image.jpg will return an ImageArtifact contianing the image bytes in PNG format.
            Defaults to PNG.
    """

    format: str = field(default="PNG", kw_only=True)

    def load(self, path: str | Path) -> ImageArtifact:  # pyright: ignore
        return self.file_to_artifact(path)

    def load_collection(self, paths: list[str | Path]) -> dict[str, ImageArtifact]:  # pyright: ignore
        return utils.execute_futures_dict(
            {utils.str_to_hash(str(path)): self.futures_executor.submit(self.file_to_artifact, path) for path in paths}
        )

    def file_to_artifact(self, path: str | Path) -> ImageArtifact:
        file_name = os.path.basename(path)
        dir_name = os.path.dirname(path)

        image = Image.open(path)

        # Ensure image is in the specified format.
        byte_stream = BytesIO()
        image.save(byte_stream, format=self.format)

        return ImageArtifact(
            byte_stream.getvalue(),
            name=file_name,
            dir_name=dir_name,
            mime_type=f"image/{self.format.lower()}",
            width=image.width,
            height=image.height,
        )

format: str = field(default='PNG', kw_only=True) class-attribute instance-attribute

file_to_artifact(path)

Source code in griptape/griptape/loaders/image_loader.py
def file_to_artifact(self, path: str | Path) -> ImageArtifact:
    file_name = os.path.basename(path)
    dir_name = os.path.dirname(path)

    image = Image.open(path)

    # Ensure image is in the specified format.
    byte_stream = BytesIO()
    image.save(byte_stream, format=self.format)

    return ImageArtifact(
        byte_stream.getvalue(),
        name=file_name,
        dir_name=dir_name,
        mime_type=f"image/{self.format.lower()}",
        width=image.width,
        height=image.height,
    )

load(path)

Source code in griptape/griptape/loaders/image_loader.py
def load(self, path: str | Path) -> ImageArtifact:  # pyright: ignore
    return self.file_to_artifact(path)

load_collection(paths)

Source code in griptape/griptape/loaders/image_loader.py
def load_collection(self, paths: list[str | Path]) -> dict[str, ImageArtifact]:  # pyright: ignore
    return utils.execute_futures_dict(
        {utils.str_to_hash(str(path)): self.futures_executor.submit(self.file_to_artifact, path) for path in paths}
    )