Implementation:Kornia Kornia Generate Examples
| Knowledge Sources | |
|---|---|
| Domains | Documentation, Infrastructure |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
This script generates all the example images used in the Kornia documentation by programmatically applying augmentations, color transforms, filters, morphological operations, geometric transforms, and feature detectors to sample images.
Description
The generate_examples.py file in the Kornia docs directory is a standalone script that produces the static example images for the Sphinx documentation. Its main() function downloads sample images from GitHub, then iterates over dictionaries of transforms organized by Kornia submodule: augmentations (kornia.augmentation), mix augmentations, mask augmentations, augmentation containers (AugmentationSequential, PatchSequential), color transforms (kornia.color), colormaps (kornia.color.ColorMap), enhance operations (kornia.enhance), morphology operations (kornia.morphology), filters (kornia.filters), geometric transforms (kornia.geometry.transform), and local feature detectors/response functions (kornia.feature). For each transform, it generates side-by-side comparison images (original + transformed) and saves them as PNG files. Helper functions include download_tutorials_examples for fetching tutorial scripts, read_img_from_url for loading images from URLs, transparent_pad for RGBA padding, and draw_bbox_kpts for drawing bounding boxes and keypoints.
Usage
This script is executed during Sphinx documentation builds (called from docs/source/conf.py) and can also be run standalone via python docs/generate_examples.py. It produces images in docs/source/_static/img/.
Code Reference
Source Location
- Repository: Kornia
- File: docs/generate_examples.py
- Lines: 1-724
Key Signatures
def download_tutorials_examples(download_infos: dict[str, str], directory: Path): ...
def read_img_from_url(url: str, resize_to: Optional[tuple[int, int]] = None,
**resize_kwargs) -> torch.Tensor: ...
def transparent_pad(src: torch.Tensor, shape: tuple[int, int]) -> torch.Tensor: ...
def draw_bbox_kpts(imgs: torch.Tensor, bboxes: torch.Tensor,
keypoints: torch.Tensor) -> torch.Tensor: ...
def main(): ...
Import
# Typically run as a script, not imported directly
# python docs/generate_examples.py
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (none) | - | - | The script fetches images from hardcoded GitHub URLs and applies transforms using Kornia APIs. |
Outputs
| Name | Type | Description |
|---|---|---|
| PNG images | Files | Generated example images saved to docs/source/_static/img/ and tutorial scripts to docs/source/_static/scripts/. |
Generated Example Categories
| Category | Module | Count | Description |
|---|---|---|---|
| Augmentations | kornia.augmentation | ~40 | RandomAffine, RandomCrop, RandomGaussianBlur, etc. |
| Mix Augmentations | kornia.augmentation | 2 | RandomMixUpV2, RandomCutMixV2 |
| Mask Augmentations | kornia.augmentation | 1 | RandomTransplantation |
| Containers | kornia.augmentation | 2 | AugmentationSequential, PatchSequential |
| Color Transforms | kornia.color | ~13 | rgb_to_grayscale, rgb_to_hsv, apply_colormap, etc. |
| Colormaps | kornia.color | 1+ | ColorMapType visualization |
| Enhance | kornia.enhance | ~13 | adjust_brightness, equalize, sharpness, etc. |
| Morphology | kornia.morphology | 7 | dilation, erosion, opening, closing, etc. |
| Filters | kornia.filters | ~14 | gaussian_blur2d, sobel, canny, etc. |
| Geometry | kornia.geometry.transform | ~16 | rotate, scale, warp_affine, elastic_transform2d, etc. |
| Features | kornia.feature | 6 | harris_response, DISK, KeyNet, etc. |
Usage Examples
# Run from command line to regenerate all documentation images
# cd /path/to/kornia
# python docs/generate_examples.py
# Or use specific helpers
from docs.generate_examples import read_img_from_url
img = read_img_from_url("https://raw.githubusercontent.com/kornia/data/main/panda.jpg")
# img is a torch.Tensor of shape (1, 3, H, W) normalized to [0, 1]