From 1eb048d22ab40d53a7126093e1427f47db9eab81 Mon Sep 17 00:00:00 2001
From: Yannick DAYER <yannick.dayer@idiap.ch>
Date: Mon, 27 May 2024 12:10:04 +0200
Subject: [PATCH] fix: adapt to PBM loading returning boolean values

Loading PBM files with imageio now returns an array of boolean.
This converts the image to uint8 with 0 or 255 values like before.
---
 src/bob/io/base/__init__.py | 4 ++++
 tests/test_image_support.py | 4 ++--
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/bob/io/base/__init__.py b/src/bob/io/base/__init__.py
index 99be55a..6dd12cd 100644
--- a/src/bob/io/base/__init__.py
+++ b/src/bob/io/base/__init__.py
@@ -112,6 +112,10 @@ def open_file(filename) -> np.ndarray:
             if img.shape[-1] == 1:
                 img = img.squeeze(-1)
 
+        # PBMs return a boolean array; Convert it to 0 or 255 values
+        if extension.lower() == ".pbm" and img.dtype == bool:
+            img = img.astype(np.uint8) * 255
+
         img = check_gray(img)
         return img if img.ndim == 2 else to_bob(img)
     else:
diff --git a/tests/test_image_support.py b/tests/test_image_support.py
index ad7ef82..3e2970d 100644
--- a/tests/test_image_support.py
+++ b/tests/test_image_support.py
@@ -106,7 +106,7 @@ def test_netpbm():
     transcode(datafile("test.pgm", __name__))  # indexed, works fine
     transcode(datafile("test.ppm", __name__))  # indexed, works fine
     transcode(datafile("test_2.pgm", __name__))  # indexed, works fine
-    # transcode(datafile("test_2.ppm", __name__))  # indexed, works fine ----> THIS DOES NOT WORK
+    transcode(datafile("test_2.ppm", __name__))  # indexed, works fine
     transcode(datafile("test_spaces.pgm", __name__))  # indexed, works fine
 
     # transcode(datafile("test.jpg", __name__))  # does not work
@@ -153,6 +153,6 @@ def test_image_exceptions():
     # Real GRAY PNG image
     transcode(datafile("read_png_gray.png", __name__))
 
-    # Trucated JPEG
+    # Truncated JPEG
     # THIS TEST FAILS
     # transcode(datafile("truncated_jpeg.jpg", __name__))
-- 
GitLab