Skip to content

Commit

Permalink
TST: Upgrade test fro byte str
Browse files Browse the repository at this point in the history
  • Loading branch information
jwboth committed Apr 17, 2024
1 parent 4214171 commit 02adaa2
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions tests/unit/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Expand Down

0 comments on commit 02adaa2

Please sign in to comment.