Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fix/rgba png loading (#3608)

Yashh901933061310
diff --git a/kornia/io/io.py b/kornia/io/io.py
index 5cec60c30..4766a0f0a 100644
--- a/kornia/io/io.py
+++ b/kornia/io/io.py
@@ -40,6 +40,34 @@ class ImageLoadType(Enum):
RGB32 = 5
+_PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n"
+
+
+def _read_png_color_type(path_file: Path) -> int | None:
+ """Read the color type byte from a PNG file header.
+
+ Returns None if the file is truncated or has an invalid PNG signature.
+ PNG color types: 0=Grayscale, 2=RGB, 3=Indexed, 4=Grayscale+Alpha, 6=RGBA.
+ """
+ with open(path_file, "rb") as f:
+ # Need 26 bytes: 8 (signature) + 8 (IHDR chunk header) + 8 (width/height) + 1 (bit depth) + 1 (color type)
+ header = f.read(26)
+ if len(header) < 26 or header[:8] != _PNG_SIGNATURE:
+ return None
+ return header[25]
+
+
+# Map PNG color type byte to kornia_rs read mode.
+# Types not listed here fall through to read_image_any (which handles RGB).
+# Note: color type 4 (Grayscale+Alpha) is intentionally omitted β€” kornia_rs
+# does not support it, so it falls through to read_image_any as RGB.
+_PNG_COLOR_TYPE_TO_MODE: dict[int, str] = {
+ 0: "mono", # Grayscale
+ 3: "mono", # Indexed (palette) β€” decoded as single channel by kornia_rs
+ 6: "rgba", # RGBA
+}
+
+
def _load_image_to_tensor(path_file: Path, device: Union[str, torch.device, None]) -> torch.Tensor:
"""Read an image file and decode using the Kornia Rust backend.
@@ -56,6 +84,15 @@ def _load_image_to_tensor(path_file: Path, device: Union[str, torch.device, None
# read image and return as `np.ndarray` with shape HxWxC
if path_file.suffix.lower() in [".jpg", ".jpeg"]:
img = kornia_rs.read_image_jpegturbo(str(path_file))
+ elif path_file.suffix.lower() == ".png":
+ color_type = _read_png_color_type(path_file)
+ # None (truncated/invalid header) intentionally falls through to read_image_any
+ mode = _PNG_COLOR_TYPE_TO_MODE.get(color_type)
+ if mode is None or mode == "rgb":
+ # RGB is the default for read_image_any; use it for unknown types too
+ img = kornia_rs.read_image_any(str(path_file))
+ else:
+ img = kornia_rs.read_image_png_u8(str(path_file), mode)
else:
img = kornia_rs.read_image_any(str(path_file))
@@ -80,6 +117,45 @@ def _to_uint8(image: torch.Tensor) -> torch.Tensor:
return image.mul(255.0).byte()
+def _convert_image_type(image: torch.Tensor, desired_type: ImageLoadType) -> torch.Tensor:
+ """Convert a raw CxHxW uint8 image tensor to the desired type."""
+ channels = image.shape[0]
+
+ if desired_type == ImageLoadType.UNCHANGED:
+ return image
+
+ # Normalize RGBA to RGB for types that don't need alpha
+ if channels == 4 and desired_type != ImageLoadType.RGBA8:
+ image = _to_uint8(kornia.color.rgba_to_rgb(_to_float32(image)))
+ channels = 3
+
+ match (desired_type, channels):
+ case (ImageLoadType.GRAY8, 1):
+ return image
+ case (ImageLoadType.GRAY8, 3):
+ return kornia.color.rgb_to_grayscale(image)
+ case (ImageLoadType.RGB8, 3):
+ return image
+ case (ImageLoadType.RGB8, 1):
+ return kornia.color.grayscale_to_rgb(image)
+ case (ImageLoadType.RGBA8, 4):
+ return image
+ case (ImageLoadType.RGBA8, 3):
+ return _to_uint8(kornia.color.rgb_to_rgba(_to_float32(image), 0.0))
+ case (ImageLoadType.RGBA8, 1):
+ return _to_uint8(kornia.color.rgb_to_rgba(kornia.color.grayscale_to_rgb(_to_float32(image)), 0.0))
+ case (ImageLoadType.GRAY32, 1):
+ return _to_float32(image)
+ case (ImageLoadType.GRAY32, 3):
+ return kornia.color.rgb_to_grayscale(_to_float32(image))
+ case (ImageLoadType.RGB32, 3):
+ return _to_float32(image)
+ case (ImageLoadType.RGB32, 1):
+ return kornia.color.grayscale_to_rgb(_to_float32(image))
+ case _:
+ raise NotImplementedError(f"Unsupported conversion: {channels} channels to {desired_type}")
+
+
def load_image(
path_file: str | Path,
desired_type: ImageLoadType = ImageLoadType.RGB32,
@@ -102,48 +178,7 @@ def load_image(
# read the image using the kornia_rs package
image: torch.Tensor = _load_image_to_tensor(path_file, device) # CxHxW
- if desired_type == ImageLoadType.UNCHANGED:
- return image
- elif desired_type == ImageLoadType.GRAY8:
- if image.shape[0] == 1 and image.dtype == torch.uint8:
- return image
- elif image.shape[0] == 3 and image.dtype == torch.uint8:
- gray8 = kornia.color.rgb_to_grayscale(image)
- return gray8
- elif image.shape[0] == 4 and image.dtype == torch.uint8:
- gray32 = kornia.color.rgb_to_grayscale(kornia.color.rgba_to_rgb(_to_float32(image)))
- return _to_uint8(gray32)
-
- elif desired_type == ImageLoadType.RGB8:
- if image.shape[0] == 3 and image.dtype == torch.uint8:
- return image
- elif image.shape[0] == 1 and image.dtype == torch.uint8:
- rgb8 = kornia.color.grayscale_to_rgb(image)
- return rgb8
-
- elif desired_type == ImageLoadType.RGBA8:
- if image.shape[0] == 3 and image.dtype == torch.uint8:
- rgba32 = kornia.color.rgb_to_rgba(_to_float32(image), 0.0)
- return _to_uint8(rgba32)
-
- elif desired_type == ImageLoadType.GRAY32:
- if image.shape[0] == 1 and image.dtype == torch.uint8:
- return _to_float32(image)
- elif image.shape[0] == 3 and image.dtype == torch.uint8:
- gray32 = kornia.color.rgb_to_grayscale(_to_float32(image))
- return gray32
- elif image.shape[0] == 4 and image.dtype == torch.uint8:
- gray32 = kornia.color.rgb_to_grayscale(kornia.color.rgba_to_rgb(_to_float32(image)))
- return gray32
-
- elif desired_type == ImageLoadType.RGB32:
- if image.shape[0] == 3 and image.dtype == torch.uint8:
- return _to_float32(image)
- elif image.shape[0] == 1 and image.dtype == torch.uint8:
- rgb32 = kornia.color.grayscale_to_rgb(_to_float32(image))
- return rgb32
-
- raise NotImplementedError(f"Unknown type: {desired_type}")
+ return _convert_image_type(image, desired_type)
def _write_uint8_image(path_file: Path, img_np: Any, quality: int) -> None:
diff --git a/tests/io/test_io_image.py b/tests/io/test_io_image.py
index 132ebfde3..82b8dc28f 100644
--- a/tests/io/test_io_image.py
+++ b/tests/io/test_io_image.py
@@ -66,6 +66,15 @@ def png_image(tmp_path_factory):
return filename
[email protected](scope="session")
+def rgba_png_image(tmp_path_factory):
+ """Create an RGBA PNG image for testing."""
+ filename = tmp_path_factory.mktemp("data") / "rgba_image.png"
+ img_rgba = np.random.randint(0, 255, (32, 32, 4), dtype=np.uint8) # noqa: NPY002
+ kornia_rs.write_image_png_u8(str(filename), img_rgba, mode="rgba")
+ return filename
+
+
@pytest.fixture(scope="session")
def jpg_image(tmp_path_factory):
url = "https://github.com/kornia/data/raw/main/crowd.jpg"
@@ -131,6 +140,22 @@ class TestIoImage:
assert img.shape[0] == expected_channels
assert img.dtype == expected_type
+ @pytest.mark.parametrize(
+ "load_type,expected_type,expected_channels",
+ [
+ (ImageLoadType.UNCHANGED, torch.uint8, 4),
+ (ImageLoadType.GRAY8, torch.uint8, 1),
+ (ImageLoadType.GRAY32, torch.float32, 1),
+ (ImageLoadType.RGB8, torch.uint8, 3),
+ (ImageLoadType.RGBA8, torch.uint8, 4),
+ (ImageLoadType.RGB32, torch.float32, 3),
+ ],
+ )
+ def test_load_rgba_png(self, rgba_png_image, load_type, expected_type, expected_channels):
+ img = load_image(rgba_png_image, load_type)
+ assert img.shape[0] == expected_channels
+ assert img.dtype == expected_type
+
@pytest.mark.parametrize("ext", ["jpg"])
@pytest.mark.parametrize("channels", [3])
def test_write_image(self, device, tmp_path, ext, channels):