kornia.io#

Package to load and save image data.

The package internally implements kornia_rs which contains a low level implementation for Computer Vision in the Rust language. In addition, we implement the DLPack protocol natively in Rust to reduce the memory footprint during the decoding and types conversion.

Tip

You need to pip install kornia_rs to use this package. For now we only support Linux platforms. Contact us or sponsor the project for more support (mac, win, rust, c++, video and camera). See: https://opencollective.com/kornia

Note

The package needs at least PyTorch 1.10.0 installed.

import kornia as K
from kornia.io import ImageLoadType
from kornia.core import Tensor

img: Tensor = K.io.load_image(file_path, ImageLoadType.UNCHANGED, device="cuda")
# will load CxHxW / in the original format in "cuda"

img: Tensor = K.io.load_image(file_path, ImageLoadType.RGB8, device="cpu")
# will load 3xHxW / in torch.uint in range [0,255] in "cpu"

img: Tensor = K.io.load_image(file_path, ImageLoadType.GRAY8, device="cuda")
# will load 1xHxW / in torch.uint8 in range [0,255] in "cuda"

img: Tensor = K.io.load_image(file_path, ImageLoadType.GRAY32, device="cpu")
# will load 1xHxW / in torch.float32 in range [0,1] in "cpu"

img: Tensor = K.io.load_image(file_path, ImageLoadType.RGB32, device="cuda")
# will load 3xHxW / in torch.float32 in range [0,1] in "cuda"
kornia.io.load_image(path_file, desired_type, device='cpu')#

Read an image file and decode using the Kornia Rust backend.

Parameters:
  • path_file (str | Path) – Path to a valid image file.

  • desired_type (ImageLoadType) – the desired image type, defined by color space and dtype.

  • device (Union[str, device, None], optional) – the device where you want to get your image placed. Default: "cpu"

Return type:

Tensor

Returns:

Image tensor with shape \((3,H,W)\).

kornia.io.write_image(path_file, image)#

Save an image file using the Kornia Rust backend.

For now, we only support the writing of JPEG of the following types: RGB8.

Parameters:
  • path_file (str | Path) – Path to a valid image file.

  • image (Tensor) – Image tensor with shape \((3,H,W)\).

Return type:

None

Returns:

None.

class kornia.io.ImageLoadType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)#

Enum to specify the desired image type.

GRAY32 = 4#
GRAY8 = 1#
RGB32 = 5#
RGB8 = 2#
RGBA8 = 3#
UNCHANGED = 0#