diff --git a/tests/unit/test_io.py b/tests/unit/test_io.py index 3ba94a0d..2af81385 100644 --- a/tests/unit/test_io.py +++ b/tests/unit/test_io.py @@ -28,16 +28,23 @@ def test_imread_from_numpy(): path.unlink() -@pytest.mark.parametrize("shape", [(10, 20, 3), (10, 20)]) +@pytest.mark.parametrize("shape", [(10, 20, 3), (10, 20, 1), (10, 20)]) def test_imread_from_bytes(shape): """Test imread for bytes images, for RGB and grayscale type images.""" - # Generate array equivalent with 255 values - array = 255 * np.ones(shape, dtype=np.uint8) + # Generate array equivalent with random values + pre_array = (np.random.rand(*shape) * 255).astype(np.uint8) - # Save array as jpg using cv2 - path = Path("random_distribution.jpg") - cv2.imwrite(str(path), array) + # Convert array to BGR format + if len(shape) == 3 and shape[2] == 3: + array = cv2.cvtColor(pre_array, cv2.COLOR_RGB2BGR) + else: + array = pre_array.copy() + + # Save array lossfree using cv2 + path = Path("random_distribution.png") + cv2.imwrite(str(path), array, [cv2.IMWRITE_PNG_COMPRESSION, 0]) + print(Path(".").resolve()) # Read numpy image with open(path, "rb") as file: @@ -46,7 +53,7 @@ def test_imread_from_bytes(shape): bytes_image = darsia.imread_from_bytes(byte_str, dim=2, width=2, height=1) # Compare arrays. - assert np.allclose(bytes_image.img, array) + assert np.allclose(bytes_image.img.reshape(pre_array.shape), pre_array) # Clean up path.unlink()