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