Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

upgrade kornia-rs to 0.1.9 (#3166)

Edgar Riba9e771925ecfa
diff --git a/kornia/io/io.py b/kornia/io/io.py
index 13391f51e..9ec6039ba 100644
--- a/kornia/io/io.py
+++ b/kornia/io/io.py
@@ -19,6 +19,7 @@ from __future__ import annotations
from enum import Enum
from pathlib import Path
+from typing import Any
import kornia_rs
import torch
@@ -55,7 +56,7 @@ def _load_image_to_tensor(path_file: Path, device: Device) -> Tensor:
"""
# read image and return as `np.ndarray` with shape HxWxC
if path_file.suffix.lower() in [".jpg", ".jpeg"]:
- img = kornia_rs.read_image_jpeg(str(path_file))
+ img = kornia_rs.read_image_jpegturbo(str(path_file))
else:
img = kornia_rs.read_image_any(str(path_file))
@@ -144,36 +145,69 @@ def load_image(
raise NotImplementedError(f"Unknown type: {desired_type}")
-def write_image(path_file: str | Path, image: Tensor) -> None:
- """Save an image file using the Kornia Rust backend.
+def _write_uint8_image(path_file: Path, img_np: Any, quality: int) -> None:
+ """Write uint8 image to file."""
+ if path_file.suffix.lower() in [".jpg", ".jpeg"]:
+ mode = "mono" if img_np.ndim == 2 or (img_np.ndim == 3 and img_np.shape[-1] == 1) else "rgb"
+ kornia_rs.write_image_jpeg(str(path_file), img_np, mode=mode, quality=quality)
+ elif path_file.suffix.lower() == ".png":
+ mode = "mono" if img_np.ndim == 2 or (img_np.ndim == 3 and img_np.shape[-1] == 1) else "rgb"
+ kornia_rs.write_image_png_u8(str(path_file), img_np, mode=mode)
+ elif path_file.suffix.lower() == ".tiff":
+ mode = "mono" if img_np.ndim == 2 or (img_np.ndim == 3 and img_np.shape[-1] == 1) else "rgb"
+ kornia_rs.write_image_tiff_u8(str(path_file), img_np, mode=mode)
+ else:
+ raise NotImplementedError(f"Unsupported file extension: {path_file.suffix} for uint8 image")
+
+
+def _write_uint16_image(path_file: Path, img_np: Any) -> None:
+ """Write uint16 image to file."""
+ mode = "mono" if img_np.ndim == 2 or (img_np.ndim == 3 and img_np.shape[-1] == 1) else "rgb"
+ if path_file.suffix.lower() == ".png":
+ kornia_rs.write_image_png_u16(str(path_file), img_np, mode=mode)
+ elif path_file.suffix.lower() == ".tiff":
+ kornia_rs.write_image_tiff_u16(str(path_file), img_np, mode=mode)
+ else:
+ raise NotImplementedError(f"Unsupported file extension: {path_file.suffix} for uint16 image")
+
- For now, we only support the writing of JPEG of the following types: RGB8.
+def _write_float32_image(path_file: Path, img_np: Any) -> None:
+ """Write float32 image to file."""
+ mode = "mono" if img_np.ndim == 2 or (img_np.ndim == 3 and img_np.shape[-1] == 1) else "rgb"
+ if path_file.suffix.lower() == ".tiff":
+ kornia_rs.write_image_tiff_f32(str(path_file), img_np, mode=mode)
+ else:
+ raise NotImplementedError(f"Unsupported file extension: {path_file.suffix} for float32 image")
+
+
+def write_image(path_file: str | Path, image: Tensor, quality: int = 80) -> None:
+ """Save an image file using the Kornia Rust backend.
Args:
path_file: Path to a valid image file.
image: Image tensor with shape :math:`(3,H,W)`, `(1,H,W)` and `(H,W)`.
+ quality: The quality of the JPEG encoding. If the file extension is .png or .tiff, the quality is ignored.
Return:
None.
-
"""
if not isinstance(path_file, Path):
path_file = Path(path_file)
KORNIA_CHECK(
- path_file.suffix in [".jpg", ".jpeg"],
- f"Invalid file extension: {path_file}, only .jpg and .jpeg are supported.",
+ path_file.suffix in [".jpg", ".jpeg", ".png", ".tiff"],
+ f"Invalid file extension: {path_file}, only .jpg, .jpeg, .png and .tiff are supported.",
)
- if (image.dim() == 3 and image.shape[0] == 1) or image.dim() == 2: # Grayscale image
- image = image.squeeze(0) # HxW
- image = image[None, :, :].repeat(3, 1, 1) # Convert to HxWx3
-
- KORNIA_CHECK(image.dim() == 3 and image.shape[0] == 3, f"Invalid image shape: {image.shape}")
- KORNIA_CHECK(image.dtype == torch.uint8, f"Invalid image dtype: {image.dtype}")
+ KORNIA_CHECK(image.dim() >= 2, f"Invalid image shape: {image.shape}. Must be at least 2D.")
- # convert the tensor to numpy
- img_np = tensor_to_image(image, keepdim=True, force_contiguous=True) # HxWx3
+ img_np = tensor_to_image(image, keepdim=True, force_contiguous=True) # HxWxC
- # save the image using the
- kornia_rs.write_image_jpeg(str(path_file), img_np)
+ if image.dtype == torch.uint8:
+ _write_uint8_image(path_file, img_np, quality)
+ elif image.dtype == torch.uint16:
+ _write_uint16_image(path_file, img_np)
+ elif image.dtype == torch.float32:
+ _write_float32_image(path_file, img_np)
+ else:
+ raise NotImplementedError(f"Unsupported image dtype: {image.dtype}")
diff --git a/requirements/requirements.txt b/requirements/requirements.txt
index e6271a6fa..730b0ba93 100644
--- a/requirements/requirements.txt
+++ b/requirements/requirements.txt
@@ -1,3 +1,3 @@
-kornia_rs>=0.1.0
+kornia_rs>=0.1.9
packaging
torch>=1.9.1