haritsahm
add test script
fde18f6
raw
history blame
991 Bytes
import tempfile
import cv2
import numpy as np
import pytest
from PIL import Image
@pytest.mark.parametrize('dtype,extension,dim', [
(np.uint8, '.png', 1),
(np.uint8, '.jpg', 3),
(np.uint8, '.png', 1),
(np.uint16, '.png', 1),
(np.uint16, '.jpg', 1),
(np.float32, '.tiff', 1),
(np.uint8, '.tiff', 3),
]
)
def test_image_reader(dtype, extension, dim):
tf = tempfile.NamedTemporaryFile(suffix=extension)
if dtype in [np.uint8, np.uint16]:
image_w = np.random.randint(0, 225, (225, 225, dim)).astype(dtype)
else:
image_w = np.random.rand(225, 225, dim).astype(dtype)
if dim == 1:
image_w = np.squeeze(image_w)
cv2.imwrite(tf.name, image_w)
image_r = np.array(Image.open(tf.name))
image_r = cv2.normalize(
image_r, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U)
assert image_r.dtype == np.uint8
assert image_r.shape[:2] == (225, 225)
assert np.min(image_r) >= 0 and np.max(image_r) <= 255