Implementation:Kornia Kornia Sepia Filter
| Knowledge Sources | |
|---|---|
| Domains | Vision, Color_Processing |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
This module applies a sepia tone filter to RGB image tensors using a fixed color transformation matrix.
Description
sepia.py is a module in the Kornia library's color subpackage that implements a sepia tone filter for RGB images. The sepia_from_rgb function applies a standard sepia transformation matrix to the RGB channels: R_out = 0.393*R + 0.769*G + 0.189*B, G_out = 0.349*R + 0.686*G + 0.168*B, B_out = 0.272*R + 0.534*G + 0.131*B. An optional rescaling step normalizes the output so that the maximum pixel value is 1.0 (or 255 for integer inputs). The Sepia nn.Module class wraps this function with configurable rescale and epsilon parameters.
Usage
Import this module when you need to apply a vintage sepia tone effect to images in a differentiable pipeline, for example in image style transfer, augmentation, or artistic filter applications.
Code Reference
Source Location
- Repository: Kornia
- File: kornia/color/sepia.py
- Lines: 1-97
Signature
def sepia_from_rgb(input: torch.Tensor, rescale: bool = True, eps: float = 1e-6) -> torch.Tensor: ...
class Sepia(nn.Module):
def __init__(self, rescale: bool = True, eps: float = 1e-6) -> None: ...
def forward(self, input: torch.Tensor) -> torch.Tensor: ...
Import
from kornia.color import sepia_from_rgb, Sepia
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| input | torch.Tensor | Yes | RGB image with shape (*, 3, H, W). |
| rescale | bool | No | If True, rescale the output so the max value is 1.0. Default: True. |
| eps | float | No | Epsilon for numerical stability during rescaling. Default: 1e-6. |
Outputs
| Name | Type | Description |
|---|---|---|
| return | torch.Tensor | Sepia-toned image with shape (*, 3, H, W). If rescale=True, values are normalized to max 1.0. If rescale=False, values may exceed 1.0. |
Usage Examples
Basic Usage
import torch
from kornia.color import sepia_from_rgb, Sepia
# Apply sepia filter with rescaling (default)
rgb = torch.rand(1, 3, 256, 256)
sepia_img = sepia_from_rgb(rgb)
print(sepia_img.shape) # torch.Size([1, 3, 256, 256])
print(sepia_img.max()) # approximately 1.0 due to rescaling
# Apply sepia filter without rescaling
sepia_raw = sepia_from_rgb(rgb, rescale=False)
print(sepia_raw.max()) # may exceed 1.0
# Using the nn.Module wrapper
sepia_module = Sepia(rescale=True, eps=1e-6)
sepia_out = sepia_module(rgb)
print(sepia_out.shape) # torch.Size([1, 3, 256, 256])