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 Rgb Conversions

From Leeroopedia


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

Overview

This module provides a comprehensive set of differentiable conversions between RGB and related color representations including BGR, RGBA, linear RGB, RGB255, and surface normals.

Description

rgb.py is a module in the Kornia library's color subpackage containing functions and nn.Module classes for converting between RGB and closely related color formats. It includes:

  • RGB/BGR swap: rgb_to_bgr, bgr_to_rgb (channel flip)
  • Alpha channel: rgb_to_rgba, bgr_to_rgba, rgba_to_rgb (with alpha compositing over a configurable background), rgba_to_bgr
  • Gamma correction: rgb_to_linear_rgb (sRGB to linear), linear_rgb_to_rgb (linear to sRGB)
  • Quantization: rgb_to_rgb255, rgb255_to_rgb (scale between [0,1] and [0,255])
  • Surface normals: normals_to_rgb255, rgb255_to_normals (mapping between normal vectors and visualization)

Each function has a corresponding nn.Module wrapper class (e.g., BgrToRgb, RgbToBgr, RgbToRgba, etc.) for integration in PyTorch pipelines.

Usage

Import this module when you need to swap color channels (RGB/BGR), add or remove alpha channels, convert between sRGB gamma-corrected and linear RGB, quantize to 8-bit range, or convert surface normal maps to and from visualization formats.

Code Reference

Source Location

Signature

def rgb_to_bgr(image: torch.Tensor) -> torch.Tensor: ...
def bgr_to_rgb(image: torch.Tensor) -> torch.Tensor: ...
def rgb_to_rgba(image: torch.Tensor, alpha_val: Union[float, torch.Tensor]) -> torch.Tensor: ...
def bgr_to_rgba(image: torch.Tensor, alpha_val: Union[float, torch.Tensor]) -> torch.Tensor: ...
def rgba_to_rgb(image: torch.Tensor, background_color: Optional[torch.Tensor] = None) -> torch.Tensor: ...
def rgba_to_bgr(image: torch.Tensor) -> torch.Tensor: ...
def rgb_to_linear_rgb(image: torch.Tensor) -> torch.Tensor: ...
def linear_rgb_to_rgb(image: torch.Tensor) -> torch.Tensor: ...
def normals_to_rgb255(image: torch.Tensor) -> torch.Tensor: ...
def rgb_to_rgb255(image: torch.Tensor) -> torch.Tensor: ...
def rgb255_to_rgb(image: torch.Tensor) -> torch.Tensor: ...
def rgb255_to_normals(image: torch.Tensor) -> torch.Tensor: ...

class BgrToRgb(nn.Module): ...
class RgbToBgr(nn.Module): ...
class RgbToRgba(nn.Module): ...
class BgrToRgba(nn.Module): ...
class RgbaToRgb(nn.Module): ...
class RgbaToBgr(nn.Module): ...
class RgbToLinearRgb(nn.Module): ...
class LinearRgbToRgb(nn.Module): ...
class NormalsToRgb255(nn.Module): ...
class RgbToRgb255(nn.Module): ...
class Rgb255ToRgb(nn.Module): ...
class Rgb255ToNormals(nn.Module): ...

Import

from kornia.color import (
    rgb_to_bgr, bgr_to_rgb,
    rgb_to_rgba, rgba_to_rgb,
    rgb_to_linear_rgb, linear_rgb_to_rgb,
    rgb_to_rgb255, rgb255_to_rgb,
)

I/O Contract

Inputs

Name Type Required Description
image (rgb_to_bgr, bgr_to_rgb) torch.Tensor Yes Image with shape (*, 3, H, W).
image (rgb_to_rgba) torch.Tensor Yes RGB image with shape (*, 3, H, W).
alpha_val (rgb_to_rgba) Union[float, torch.Tensor] Yes Scalar float or tensor of shape (*, 1, H, W) for the alpha channel.
image (rgba_to_rgb) torch.Tensor Yes RGBA image with shape (*, 4, H, W).
background_color (rgba_to_rgb) Optional[torch.Tensor] No Background color for alpha compositing. Tuple/list of 3 floats, a tensor of shape (*, 3, H, W), or None (white default).
image (rgb_to_linear_rgb) torch.Tensor Yes sRGB image with shape (*, 3, H, W), values in [0, 1].
image (linear_rgb_to_rgb) torch.Tensor Yes Linear RGB image with shape (*, 3, H, W).
image (rgb_to_rgb255) torch.Tensor Yes RGB image with shape (*, 3, H, W), values in [0, 1].
image (rgb255_to_rgb) torch.Tensor Yes RGB image with shape (*, 3, H, W), values in [0, 255].

Outputs

Name Type Description
rgb_to_bgr / bgr_to_rgb torch.Tensor Image with flipped channels, shape (*, 3, H, W).
rgb_to_rgba torch.Tensor RGBA image with shape (*, 4, H, W).
rgba_to_rgb torch.Tensor Alpha-composited RGB image with shape (*, 3, H, W).
rgb_to_linear_rgb torch.Tensor Linear RGB image with shape (*, 3, H, W).
linear_rgb_to_rgb torch.Tensor sRGB image with shape (*, 3, H, W).
rgb_to_rgb255 torch.Tensor Image scaled to [0, 255] range, shape (*, 3, H, W).
rgb255_to_rgb torch.Tensor Image scaled to [0, 1] range, shape (*, 3, H, W).

Usage Examples

Basic Usage

import torch
from kornia.color import rgb_to_bgr, rgb_to_rgba, rgba_to_rgb, rgb_to_linear_rgb

# Convert RGB to BGR
rgb_img = torch.rand(1, 3, 256, 256)
bgr_img = rgb_to_bgr(rgb_img)
print(bgr_img.shape)  # torch.Size([1, 3, 256, 256])

# Add alpha channel to RGB image
rgba_img = rgb_to_rgba(rgb_img, alpha_val=1.0)
print(rgba_img.shape)  # torch.Size([1, 4, 256, 256])

# Convert RGBA back to RGB with alpha compositing (white background)
rgb_back = rgba_to_rgb(rgba_img)
print(rgb_back.shape)  # torch.Size([1, 3, 256, 256])

# Convert sRGB to linear RGB for colorspace operations
linear = rgb_to_linear_rgb(rgb_img)
print(linear.shape)  # torch.Size([1, 3, 256, 256])

# Using nn.Module wrappers
from kornia.color import RgbToLinearRgb
to_linear = RgbToLinearRgb()
linear_out = to_linear(rgb_img)

Related Pages

Page Connections

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