Datachain

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.read_storage("s3://bucket-name/", type="image")

ImageFile

ImageFile(**kwargs)

Bases: File

DataModel for reading image files.

Source code in datachain/lib/file.py
327
328
329
330
331
332
def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self._catalog = None
    self._caching_enabled: bool = False
    self._download_cb: Callback = DEFAULT_CALLBACK
    self._fs_path_cache: tuple[str, str, str] | None = None

get_info

get_info() -> Image

Retrieves metadata and information about the image file.

With prefetch and cache off, reads only the parts of the file it needs.

Returns:

  • Image ( Image ) –

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

Source code in datachain/lib/file.py
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
def get_info(self) -> "Image":
    """
    Retrieves metadata and information about the image file.

    With prefetch and cache off, reads only the parts of the file it needs.

    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
1222
1223
1224
1225
1226
1227
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: str | None = None, client_config: dict | None = None, *, content_type: str | None = None, content_disposition: str | None = None, cache_control: str | None = None, content_encoding: str | None = None, metadata: dict[str, str] | None = None, write_options: dict[str, Any] | None = None) -> ImageFile

Save the image to destination, optionally re-encoding to format.

If destination is a remote path, the image will be uploaded to remote storage. See :meth:File.save for full destination semantics and the object-metadata keyword arguments.

Source code in datachain/lib/file.py
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
def save(  # type: ignore[override]
    self,
    destination: str,
    format: str | None = None,
    client_config: dict | None = None,
    *,
    content_type: str | None = None,
    content_disposition: str | None = None,
    cache_control: str | None = None,
    content_encoding: str | None = None,
    metadata: dict[str, str] | None = None,
    write_options: dict[str, Any] | None = None,
) -> "ImageFile":
    """Save the image to *destination*, optionally re-encoding to *format*.

    If ``destination`` is a remote path, the image will be uploaded to
    remote storage.  See :meth:`File.save` for full destination semantics
    and the object-metadata keyword arguments.
    """
    if self._catalog is None:
        raise RuntimeError("Cannot save file: catalog is not set")
    destination = stringify_path(destination)

    # If format is not provided, determine it from the file extension
    if format is None:
        from pathlib import PurePosixPath

        from PIL import Image as PilImage

        ext = PurePosixPath(destination).suffix.lower()
        format = PilImage.registered_extensions().get(ext)

    if not format:
        raise FileError(
            f"Can't determine format for destination '{destination}'",
            self.source,
            self.path,
        )

    buf = BytesIO()
    self.read().save(buf, format=format)
    client, rel_path = self._resolve_destination(destination, client_config)
    result = client.upload(
        buf.getvalue(),
        rel_path,
        write_config=WriteConfig(
            content_type=content_type,
            content_disposition=content_disposition,
            cache_control=cache_control,
            content_encoding=content_encoding,
            metadata=metadata,
            write_options=write_options,
        ),
    )
    img = ImageFile(**result.model_dump())
    img._set_stream(self._catalog)
    return img

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.