File size: 1,150 Bytes
fde18f6 dd0a580 fde18f6 dd0a580 fde18f6 dd0a580 fde18f6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import tempfile
import cv2
import numpy as np
import pytest
from PIL import Image
INPUT_HEIGHT, INPUT_WIDTH = 600, 500
@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)
image_r = cv2.resize(
image_r, (INPUT_WIDTH, INPUT_HEIGHT), interpolation=cv2.INTER_LINEAR)
assert image_r.dtype == np.uint8
assert image_r.shape[:2] == (INPUT_HEIGHT, INPUT_WIDTH)
assert np.min(image_r) >= 0 and np.max(image_r) <= 255
|