Skip to content

ImageFile

ImageFile is inherited from File with additional methods for working with image files.

ImageFile is generated when a DataChain is created from storage, using type="image" param:

import datachain as dc

chain = dc.from_storage("s3://bucket-name/", type="image")

ImageFile

ImageFile(**kwargs)

Bases: File

DataModel for reading image files.

Source code in datachain/lib/file.py
def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self._catalog = None
    self._caching_enabled: bool = False

get_info

get_info() -> Image

Retrieves metadata and information about the image file.

Returns:

  • Image ( Image ) –

    A Model containing image metadata such as width, height and format.

Source code in datachain/lib/file.py
def get_info(self) -> "Image":
    """
    Retrieves metadata and information about the image file.

    Returns:
        Image: A Model containing image metadata such as width, height and format.
    """
    from .image import image_info

    return image_info(self)

read

read()

Returns PIL.Image.Image object.

Source code in datachain/lib/file.py
def read(self):
    """Returns `PIL.Image.Image` object."""
    from PIL import Image as PilImage

    fobj = super().read()
    return PilImage.open(BytesIO(fobj))

save

save(
    destination: str,
    format: Optional[str] = None,
    client_config: Optional[dict] = None,
)

Writes it's content to destination

Source code in datachain/lib/file.py
def save(  # type: ignore[override]
    self,
    destination: str,
    format: Optional[str] = None,
    client_config: Optional[dict] = None,
):
    """Writes it's content to destination"""
    destination = stringify_path(destination)

    client: Client = self._catalog.get_client(destination, **(client_config or {}))
    with client.fs.open(destination, mode="wb") as f:
        self.read().save(f, format=format)

Image

Bases: DataModel

A data model representing metadata for an image file.

Attributes:

  • width (int) –

    The width of the image in pixels. Defaults to -1 if unknown.

  • height (int) –

    The height of the image in pixels. Defaults to -1 if unknown.

  • format (str) –

    The format of the image file (e.g., 'jpg', 'png'). Defaults to an empty string.