Kornia-rs

0.1.10 · active · verified Fri Apr 10

Kornia-rs is a low-level computer vision library primarily written in Rust, offering high-performance image I/O, processing, and 3D operations through Python bindings. It leverages Rust's memory safety and speed for demanding computer vision tasks, integrating efficiently into machine learning and data science workflows via zero-copy data transfer with frameworks like NumPy and PyTorch. The library is actively developed by the Kornia organization and is currently at version 0.1.10.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to read an image using `kornia-rs`, convert the resulting `kornia_rs.Tensor` to a NumPy array via DLPack for seamless integration with Python data science tools, perform a basic image processing operation like resizing, and then convert the result back to a NumPy array. This highlights the library's high-performance I/O and interoperability.

import kornia_rs as K
import numpy as np
import os

# For demonstration, create a dummy image file if it doesn't exist
# In a real scenario, replace 'dummy.jpeg' with your image path.
if not os.path.exists('dummy.jpeg'):
    try:
        from PIL import Image
        img = Image.new('RGB', (256, 256), color = 'red')
        img.save('dummy.jpeg')
        print("Created 'dummy.jpeg' for quickstart.")
    except ImportError:
        print("Pillow not installed. Skipping dummy image creation.")
        print("Please create a 'dummy.jpeg' file or adjust the quickstart path.")
        exit() # Or handle gracefully without exiting

# 1. Read an image into a kornia_rs.Tensor
# This uses the image-rs backend by default (or turbojpeg if enabled/available).
image_tensor = K.read_image_jpeg('dummy.jpeg')
print(f"Original image tensor shape: {image_tensor.shape}")

# 2. Convert the kornia_rs.Tensor to a NumPy array (zero-copy via DLPack)
np_image = np.from_dlpack(image_tensor)
print(f"NumPy array shape: {np_image.shape}, dtype: {np_image.dtype}")

# 3. Perform a basic image operation, e.g., resize
# The resize function expects a kornia_rs.Tensor and returns one.
resized_tensor = K.resize(image_tensor, (128, 128), interpolation="bilinear")
print(f"Resized image tensor shape: {resized_tensor.shape}")

# Convert resized tensor back to NumPy for further processing or visualization
np_resized_image = np.from_dlpack(resized_tensor)
print(f"Resized NumPy array shape: {np_resized_image.shape}, dtype: {np_resized_image.dtype}")

# Clean up dummy image if created
if os.path.exists('dummy.jpeg') and 'Created' in locals():
    os.remove('dummy.jpeg')
    print("Removed 'dummy.jpeg'.")

view raw JSON →