Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Kornia Kornia Image Base

From Leeroopedia
Revision as of 15:21, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Kornia_Kornia_Image_Base.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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

Overview

This module defines the foundational data classes and enumerations for describing image metadata in the Kornia library, including image size, color space, pixel format, channels order, and image layout.

Description

The base module in the Kornia image package provides the core type definitions used throughout the Kornia image system. It defines five types: ImageSize (a frozen dataclass for height and width), ColorSpace (an enum with values UNKNOWN, GRAY, RGB, BGR), PixelFormat (a frozen dataclass combining color_space and bit_depth), ChannelsOrder (an enum distinguishing CHANNELS_FIRST from CHANNELS_LAST), and ImageLayout (a frozen dataclass combining image_size, channels count, and channels_order). These types are used by the Image class and other modules to enforce and communicate image structure constraints.

Usage

Import these types when constructing Image objects, when writing functions that need to reason about image layout, or when performing color space and format conversions.

Code Reference

Source Location

Signature

@dataclass(frozen=True)
class ImageSize:
    height: int | torch.Tensor
    width: int | torch.Tensor

class ColorSpace(Enum):
    UNKNOWN = 0
    GRAY = 1
    RGB = 2
    BGR = 3

@dataclass(frozen=True)
class PixelFormat:
    color_space: ColorSpace
    bit_depth: int

class ChannelsOrder(Enum):
    CHANNELS_FIRST = 0
    CHANNELS_LAST = 1

@dataclass(frozen=True)
class ImageLayout:
    image_size: ImageSize
    channels: int
    channels_order: ChannelsOrder

Import

from kornia.image.base import ImageSize, ColorSpace, PixelFormat, ChannelsOrder, ImageLayout

I/O Contract

ImageSize

Name Type Required Description
height int or torch.Tensor Yes Image height in pixels.
width int or torch.Tensor Yes Image width in pixels.

PixelFormat

Name Type Required Description
color_space ColorSpace Yes The color space (UNKNOWN, GRAY, RGB, BGR).
bit_depth int Yes Number of bits per channel (e.g., 8, 16, 32).

ImageLayout

Name Type Required Description
image_size ImageSize Yes The spatial dimensions of the image.
channels int Yes Number of image channels.
channels_order ChannelsOrder Yes Whether channels are first (C, H, W) or last (H, W, C).

Usage Examples

from kornia.image.base import ImageSize, ColorSpace, PixelFormat, ChannelsOrder, ImageLayout

# Create an image size descriptor
size = ImageSize(height=480, width=640)
assert size.height == 480
assert size.width == 640

# Create a pixel format for 8-bit RGB
pixel_format = PixelFormat(color_space=ColorSpace.RGB, bit_depth=8)
assert pixel_format.color_space == ColorSpace.RGB

# Create an image layout for channels-first (PyTorch convention)
layout = ImageLayout(
    image_size=ImageSize(480, 640),
    channels=3,
    channels_order=ChannelsOrder.CHANNELS_FIRST,
)
assert layout.channels == 3
assert layout.channels_order == ChannelsOrder.CHANNELS_FIRST

Related Pages

Page Connections

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