From 2f84e019b2e4b4201498fe72162241e9a2e41022 Mon Sep 17 00:00:00 2001
From: Yannick DAYER <yannick.dayer@idiap.ch>
Date: Mon, 27 May 2024 12:08:24 +0200
Subject: [PATCH] fix: gray PNG loading with new version of imageio.

Loading a PNG image with imageio now returns a 2-channel image instead
of 4 channels with the first three repeated.
---
 src/bob/io/base/__init__.py | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/src/bob/io/base/__init__.py b/src/bob/io/base/__init__.py
index 6b0b20d..99be55a 100644
--- a/src/bob/io/base/__init__.py
+++ b/src/bob/io/base/__init__.py
@@ -104,11 +104,13 @@ def open_file(filename) -> np.ndarray:
 
         img = imageio.imread(filename)
 
-        # PNGs have a 4th channel, which we don't want
+        # PNGs have an additional channel, which we don't want
         # Alpha channels for instance have to be ignored
         if img.ndim > 2:
-            if extension.lower() == ".png":
-                img = img[:, :, 0:3]
+            if extension.lower() == ".png" and img.shape[-1] in (2, 4):
+                img = img[:, :, :-1]
+            if img.shape[-1] == 1:
+                img = img.squeeze(-1)
 
         img = check_gray(img)
         return img if img.ndim == 2 else to_bob(img)
-- 
GitLab