Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Kornia Kornia Load Image

From Leeroopedia


Knowledge Sources
Domains Vision, IO
Last Updated 2026-02-09 15:00 GMT

Overview

Concrete tool for loading images from disk and converting to PyTorch tensors provided by the Kornia library.

Description

The load_image function reads image files using the kornia_rs Rust backend for high performance. It supports automatic color space conversion via the ImageLoadType enum, which provides the following modes:

  • RGB32 -- 3-channel RGB, float32 normalized to [0, 1]
  • GRAY32 -- 1-channel grayscale, float32 normalized to [0, 1]
  • RGB8 -- 3-channel RGB, uint8 [0, 255]
  • GRAY8 -- 1-channel grayscale, uint8 [0, 255]
  • RGBA8 -- 4-channel RGBA, uint8 [0, 255]
  • UNCHANGED -- original format preserved

The function returns tensors in (C, H, W) format with float32 [0, 1] normalization for 32-bit modes and uint8 [0, 255] for 8-bit modes. Device placement is handled automatically via the device parameter.

Usage

Import when you need to load images from disk into kornia/PyTorch pipelines. Replaces PIL/OpenCV loading with native tensor output, eliminating manual conversion steps.

Code Reference

Source Location

  • Repository: kornia
  • File: kornia/io/io.py
  • Lines: L83-146

Signature

def load_image(
    path_file: str | Path,
    desired_type: ImageLoadType = ImageLoadType.RGB32,
    device: Union[str, torch.device, None] = "cpu",
) -> torch.Tensor

Import

from kornia.io import load_image, ImageLoadType

I/O Contract

Inputs

Name Type Required Description
path_file Path Yes Path to the image file on disk
desired_type ImageLoadType No Output format and dtype; default RGB32
device torch.device | None No Target compute device; default "cpu"

Outputs

Name Type Description
return torch.Tensor Image tensor of shape (C, H, W); float32 in [0, 1] for 32-bit types, uint8 in [0, 255] for 8-bit types

Usage Examples

Basic Loading with RGB32

from kornia.io import load_image, ImageLoadType

# Load an image as float32 RGB tensor normalized to [0, 1]
img = load_image("path/to/image.jpg", ImageLoadType.RGB32)
print(img.shape)  # torch.Size([3, H, W])
print(img.dtype)   # torch.float32
print(img.min(), img.max())  # tensor(0.) tensor(1.)

Loading as Grayscale

from kornia.io import load_image, ImageLoadType

# Load as single-channel grayscale
gray = load_image("path/to/image.jpg", ImageLoadType.GRAY32)
print(gray.shape)  # torch.Size([1, H, W])

Loading to GPU

from kornia.io import load_image, ImageLoadType

# Load directly onto GPU
img_gpu = load_image("path/to/image.jpg", ImageLoadType.RGB32, device="cuda")
print(img_gpu.device)  # device(type='cuda', index=0)

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment