haritsahm commited on
Commit
fde18f6
·
1 Parent(s): 3a0c9d4

add test script

Browse files
Files changed (1) hide show
  1. tests/test_image_reader.py +37 -0
tests/test_image_reader.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tempfile
2
+
3
+ import cv2
4
+ import numpy as np
5
+ import pytest
6
+ from PIL import Image
7
+
8
+
9
+ @pytest.mark.parametrize('dtype,extension,dim', [
10
+ (np.uint8, '.png', 1),
11
+ (np.uint8, '.jpg', 3),
12
+ (np.uint8, '.png', 1),
13
+ (np.uint16, '.png', 1),
14
+ (np.uint16, '.jpg', 1),
15
+ (np.float32, '.tiff', 1),
16
+ (np.uint8, '.tiff', 3),
17
+ ]
18
+ )
19
+ def test_image_reader(dtype, extension, dim):
20
+ tf = tempfile.NamedTemporaryFile(suffix=extension)
21
+
22
+ if dtype in [np.uint8, np.uint16]:
23
+ image_w = np.random.randint(0, 225, (225, 225, dim)).astype(dtype)
24
+ else:
25
+ image_w = np.random.rand(225, 225, dim).astype(dtype)
26
+
27
+ if dim == 1:
28
+ image_w = np.squeeze(image_w)
29
+ cv2.imwrite(tf.name, image_w)
30
+
31
+ image_r = np.array(Image.open(tf.name))
32
+ image_r = cv2.normalize(
33
+ image_r, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U)
34
+
35
+ assert image_r.dtype == np.uint8
36
+ assert image_r.shape[:2] == (225, 225)
37
+ assert np.min(image_r) >= 0 and np.max(image_r) <= 255