Перейти к содержанию

Image

The Image class represents a medical image inside the framework. Although downstream code works with tensors, Image simplifies loading, visualization, and saving.

Supported file formats: .jpg / .jpeg, .png, .tif / .tiff, and .dcm (DICOM). By default, files are validated against allowed MIME types — invalid files are rejected when strict=True.

Main entry points:

  • Image.from_path(path) — load from disk
  • Image.from_base64(data) — load from a base64 string
  • image.show() — display via matplotlib (works reliably in Jupyter, unlike cv2.imshow)
  • image.save(path) — write to file
from hyppopipe.data.image import Image

image = Image.from_path("fundus.tif")
image.show()
image.save("preview.png")

Documentation

Medical and general-purpose image loading as torch tensors.

Image dataclass

Bases: DataResource

Immutable image container with optional sample metadata.

Example

Load from disk and display::

img = Image.from_path("scan.png")
img.show()

other_img = Image.from_base64("...")
other_img.show()

Attributes:

Name Type Description
body Tensor

Image tensor, typically uint8 CHW from decoders.

sample_id str | None

Optional identifier (filename stem when loaded from a path).

legend dict[str, Any] | None

Optional arbitrary metadata attached to the sample.

as_gray property

Single-channel grayscale tensor derived from body.

base64 property

Base64-encoded tensor archive (round-trips with :meth:from_base64).

base64png property

Base64-encoded PNG (decodable with OpenCV, Pillow, etc.).

shape property

Shape of body.

tensor property

Underlying image tensor.

from_base64(payload) classmethod

Load an image from base64.

Accepts payloads produced by :attr:base64 (numpy array archive) or standard base64-encoded image files (PNG, JPEG, etc.).

from_path(path, *, strict=True) classmethod

Load an image from a supported file path.

Parameters:

Name Type Description Default
path Path | str

File path (JPEG, PNG, TIFF, or DICOM).

required
strict bool

If True, verify content type matches the extension.

True

Returns:

Type Description
Self

Loaded image with sample_id set to the path stem.

Raises:

Type Description
ValueError

If the path is invalid or the format is unsupported.

save(path)

Save the image to a file.

Parameters:

Name Type Description Default
path Path | str

Path to save the image to.

required

show(**kwargs)

Display the image with matplotlib.