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 Hls Conversion

From Leeroopedia


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

Overview

This module provides differentiable conversion between RGB and HLS (Hue, Lightness, Saturation) color spaces.

Description

hls.py is a module in the Kornia library's color subpackage implementing bidirectional RGB-to-HLS color space conversion. The rgb_to_hls function computes Hue (in radians, range [0, 2*pi]), Lightness, and Saturation from an RGB image, with optimizations for both gradient-enabled (autograd) and non-gradient scenarios (using in-place operations for performance). The hls_to_rgb function converts back from HLS to RGB using an analytical formula. Both functions support arbitrary batch dimensions. Each has a corresponding nn.Module wrapper class (RgbToHls and HlsToRgb).

Usage

Import this module when you need to manipulate the hue, lightness, or saturation of images independently, for tasks such as color adjustment, augmentation, or color-based feature extraction.

Code Reference

Source Location

Signature

def rgb_to_hls(image: torch.Tensor, eps: float = 1e-8) -> torch.Tensor: ...
def hls_to_rgb(image: torch.Tensor) -> torch.Tensor: ...

class RgbToHls(nn.Module):
    def forward(self, image: torch.Tensor) -> torch.Tensor: ...

class HlsToRgb(nn.Module):
    def forward(self, image: torch.Tensor) -> torch.Tensor: ...

Import

from kornia.color import rgb_to_hls, hls_to_rgb
from kornia.color import RgbToHls, HlsToRgb

I/O Contract

Inputs

Name Type Required Description
image (rgb_to_hls) torch.Tensor Yes RGB image with shape (*, 3, H, W). Values in range (0, 1).
eps (rgb_to_hls) float No Epsilon for numerical stability in division. Default: 1e-8.
image (hls_to_rgb) torch.Tensor Yes HLS image with shape (*, 3, H, W). H in [0, 2*pi], L and S in [0, 1].

Outputs

Name Type Description
rgb_to_hls return torch.Tensor HLS image with shape (*, 3, H, W). Channel order is H (hue in radians), L (lightness), S (saturation).
hls_to_rgb return torch.Tensor RGB image with shape (*, 3, H, W).

Usage Examples

Basic Usage

import torch
from kornia.color import rgb_to_hls, hls_to_rgb

# Convert RGB to HLS
rgb = torch.rand(1, 3, 128, 128)
hls = rgb_to_hls(rgb)
print(hls.shape)  # torch.Size([1, 3, 128, 128])

# Modify lightness (channel index 1)
hls[:, 1, :, :] = hls[:, 1, :, :] * 1.2  # increase lightness by 20%

# Convert back to RGB
rgb_modified = hls_to_rgb(hls)
print(rgb_modified.shape)  # torch.Size([1, 3, 128, 128])

# Using nn.Module wrappers
from kornia.color import RgbToHls, HlsToRgb
to_hls = RgbToHls()
to_rgb = HlsToRgb()
hls_out = to_hls(rgb)
rgb_out = to_rgb(hls_out)

Related Pages

Page Connections

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