This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
test.py
78 lines (60 loc) · 2.08 KB
/
test.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import accimage
import numpy as np
import imageio
import os
ACCIMAGE_SAVE = os.environ.get('ACCIMAGE_SAVE', '')
if len(ACCIMAGE_SAVE) and ACCIMAGE_SAVE.lower() not in {'0', 'false', 'no'}:
SAVE_IMAGES = True
else:
SAVE_IMAGES = False
def image_to_np(image):
"""
Returns:
np.ndarray: Image converted to array with shape (width, height, channels)
"""
image_np = np.empty([image.channels, image.height, image.width], dtype=np.uint8)
image.copyto(image_np)
image_np = np.transpose(image_np, (1, 2, 0))
return image_np
def save_image(path, image):
imageio.imwrite(path, image_to_np(image))
def test_reading_image():
image = accimage.Image("chicago.jpg")
if SAVE_IMAGES:
save_image('test_reading_image.jpg', image)
assert image.width == 1920
assert image.height == 931
def test_reading_image_from_memory():
from_file = accimage.Image("chicago.jpg")
bytes = open("chicago.jpg", "rb").read()
from_bytes = accimage.Image(bytes)
if SAVE_IMAGES:
save_image('test_reading_image_from_memory.jpg', from_bytes)
assert from_bytes.width == 1920
assert from_bytes.height == 931
np.testing.assert_array_equal(image_to_np(from_file), image_to_np(from_bytes))
def test_resizing():
image = accimage.Image("chicago.jpg")
image.resize(size=(200, 200))
if SAVE_IMAGES:
save_image('test_resizing.jpg', image)
assert image.width == 200
assert image.height == 200
def test_cropping():
image = accimage.Image("chicago.jpg")
image.crop(box=(50, 50, 150, 150))
if SAVE_IMAGES:
save_image('test_cropping.jpg', image)
assert image.width == 100
assert image.height == 100
def test_flipping():
image = accimage.Image("chicago.jpg")
original_image_np = image_to_np(image)
FLIP_LEFT_RIGHT = 0
image.transpose(FLIP_LEFT_RIGHT)
if SAVE_IMAGES:
save_image('test_flipping.jpg', image)
new_image_np = image_to_np(image)
assert image.width == 1920
assert image.height == 931
np.testing.assert_array_equal(new_image_np[:, ::-1, :], original_image_np)