-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_converter.py
144 lines (118 loc) · 5.39 KB
/
test_converter.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
from PIL import Image
import numpy as np
from ascii_art import ImageConverter
import char_dictionary as chars
import unittest
class TestConverter(unittest.TestCase):
def test_resize(self):
converter = ImageConverter(width=1, height=1,
contrast=100, invert=False,
chars="chars/chars.npy")
img = Image.open('img_for_test/black.jpg')
resized_image = converter.resize(np.array(img), 100, 100)
new_width, new_height = resized_image.shape[0], resized_image.shape[1]
img.close()
self.assertEqual((100, 100), (new_width, new_height))
def test_get_size_in_blocks(self):
converter = ImageConverter(width=1, height=1,
contrast=100, invert=False,
chars="chars/chars.npy")
img = Image.new('L', (14, 14))
self.assertEqual(converter.get_size_in_blocks(np.array(img)), (2, 1))
def test_get_most_suitable_char(self):
converter = ImageConverter(width=1, height=1,
contrast=100, invert=False,
chars="chars/chars.npy")
img = Image.open('img_for_test/black.jpg')
char = converter.get_most_suitable_char(np.array(img), 1, 1)
img.close()
self.assertEqual(char, "W")
def test_to_ascii_char(self):
converter = ImageConverter(width=1, height=1,
contrast=100, invert=False,
chars="chars/chars.npy")
img = Image.open('img_for_test/black.jpg')
r_img = converter.resize(np.array(img),
converter.width * 7,
converter.height * 14)
ascii_img = converter.to_ascii_chars(r_img)
img.close()
self.assertEqual(ascii_img, "W")
def test_convert(self):
converter = ImageConverter(width=1, height=1,
contrast=100, invert=False,
chars="chars/chars.npy")
img = Image.open('img_for_test/black.jpg')
a_img = converter.convert(img)
img.close()
self.assertEqual(a_img, "W")
def test_to_gray_scale(self):
converter = ImageConverter(width=1, height=1,
contrast=100, invert=False,
chars="chars/chars.npy")
image = Image.open('img_for_test/black.jpg')
r_img = converter.resize(np.array(image),
converter.width,
converter.height)
img = converter.to_gray_scale(r_img)
img_arr = np.zeros((14, 28), dtype=np.int8)
image.close()
self.assertTrue((img == img_arr).all())
def test_convert_diff_color(self):
converter = ImageConverter(width=2, height=2,
contrast=0, invert=False,
chars="chars/chars.npy")
image = Image.open('img_for_test/bwg.jpg')
a_img = converter.convert(image)
image.close()
self.assertEqual(a_img, "Wj\n. ")
def test_convert_more_diff_color(self):
converter = ImageConverter(width=4, height=4,
contrast=0, invert=False,
chars="chars/chars.npy")
image = Image.open('img_for_test/bwg.jpg')
a_img = converter.convert(image)
image.close()
self.assertEqual(a_img, "WWjj\nWWjj\n.. \n.. ")
def test_lines(self):
converter = ImageConverter(width=2, height=2,
contrast=0, invert=False,
chars="chars/chars.npy")
image = Image.open('img_for_test/lines.jpg')
a_img = converter.convert(image)
image.close()
self.assertEqual(a_img, "Lr\n|j")
def test_invert(self):
converter = ImageConverter(width=1, height=1,
contrast=0, invert=True,
chars="chars/chars.npy")
image = Image.open('img_for_test/E.jpg')
a_img = converter.convert(image)
image.close()
self.assertEqual(a_img, "}")
def test_negative_contrast(self):
converter = ImageConverter(width=2, height=2,
contrast=-50, invert=False,
chars="chars/chars.npy")
image = Image.open('img_for_test/bwg.jpg')
a_img = converter.convert(image)
image.close()
self.assertEqual(a_img, "@m\n:.")
def test_positive_contrast(self):
converter = ImageConverter(width=2, height=2,
contrast=50, invert=False,
chars="chars/chars.npy")
image = Image.open('img_for_test/bwg.jpg')
a_img = converter.convert(image)
image.close()
self.assertEqual(a_img, "WJ\n ")
class TestCharDictionary(unittest.TestCase):
def test_get_char_arr(self):
char_dict = chars.CharDictionary(font='font/RobotoMono-Regular.ttf',
chars='chars/chars.npy')
char = char_dict.get_char_arr(' ')
char_arr = 255 * np.ones((14, 7), dtype=np.int8)
self.assertTrue((char == char_arr).all())
if __name__ == "__main__":
unittest.main()