|
1
|
|
|
import os |
|
2
|
|
|
import unittest |
|
3
|
|
|
|
|
4
|
|
|
from looptools import Timer |
|
5
|
|
|
|
|
6
|
|
|
from PillowImage import PillowImage |
|
7
|
|
|
from tests import * |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
View Code Duplication |
class TestPillowImageJPEG(unittest.TestCase): |
|
|
|
|
|
|
11
|
|
|
result_dir = init_result_dir('jpg') |
|
12
|
|
|
|
|
13
|
|
|
@classmethod |
|
14
|
|
|
def setUpClass(cls): |
|
15
|
|
|
cls.img_path = IMG_PATH_JPEG |
|
16
|
|
|
cls.wtrmrk_path = WTR_PATH |
|
17
|
|
|
cls.pdf = None |
|
18
|
|
|
|
|
19
|
|
|
@Timer.decorator |
|
20
|
|
|
def test_draw_text(self): |
|
21
|
|
|
"""Draw text onto a black canvas.""" |
|
22
|
|
|
with PillowImage() as draw: |
|
23
|
|
|
draw.draw_text('Here is the first text', y=10, opacity=50) |
|
24
|
|
|
draw.draw_text('Here is the second text', y=50, opacity=50) |
|
25
|
|
|
d = draw.save(destination=self.result_dir, file_name='draw_text') |
|
26
|
|
|
|
|
27
|
|
|
# Assert file exists |
|
28
|
|
|
self.assertTrue(os.path.exists(d)) |
|
29
|
|
|
return d |
|
30
|
|
|
|
|
31
|
|
|
@Timer.decorator |
|
32
|
|
|
def test_draw_img(self): |
|
33
|
|
|
"""Draw text and an image onto a black canvas.""" |
|
34
|
|
|
with PillowImage() as draw: |
|
35
|
|
|
draw.draw_img(self.img_path) |
|
36
|
|
|
draw.draw_img(self.wtrmrk_path, opacity=0.08, rotate=30) |
|
37
|
|
|
d = draw.save(destination=self.result_dir, file_name='draw_img') |
|
38
|
|
|
|
|
39
|
|
|
# Assert file exists |
|
40
|
|
|
self.assertTrue(os.path.exists(d)) |
|
41
|
|
|
return d |
|
42
|
|
|
|
|
43
|
|
|
@Timer.decorator |
|
44
|
|
|
def test_draw_img_overlay(self): |
|
45
|
|
|
"""Overlay text onto an image.""" |
|
46
|
|
|
with PillowImage(img=self.img_path) as draw: |
|
47
|
|
|
draw.draw_img(self.wtrmrk_path, opacity=0.08, rotate=30) |
|
48
|
|
|
d = draw.save(destination=self.result_dir, file_name='draw_img_overlay') |
|
49
|
|
|
|
|
50
|
|
|
# Assert file exists |
|
51
|
|
|
self.assertTrue(os.path.exists(d)) |
|
52
|
|
|
return d |
|
53
|
|
|
|
|
54
|
|
|
@Timer.decorator |
|
55
|
|
|
def test_draw_img_centered(self): |
|
56
|
|
|
"""Draw centered text onto an image.""" |
|
57
|
|
|
with PillowImage(img=self.img_path) as draw: |
|
58
|
|
|
draw.draw_img(self.wtrmrk_path, opacity=0.08, rotate=30, x='center', y='center') |
|
59
|
|
|
d = draw.save(destination=self.result_dir, file_name='draw_img_centered') |
|
60
|
|
|
|
|
61
|
|
|
# Assert file exists |
|
62
|
|
|
self.assertTrue(os.path.exists(d)) |
|
63
|
|
|
return d |
|
64
|
|
|
|
|
65
|
|
|
@Timer.decorator |
|
66
|
|
|
def test_draw_img_negbound(self): |
|
67
|
|
|
"""Draw text onto an image with a negative bound.""" |
|
68
|
|
|
with PillowImage(img=self.img_path) as draw: |
|
69
|
|
|
draw.draw_img(self.wtrmrk_path, opacity=0.08, rotate=30, x=-2000, y=-2000) |
|
70
|
|
|
d = draw.save(destination=self.result_dir, file_name='draw_img_negbound') |
|
71
|
|
|
|
|
72
|
|
|
# Assert file exists |
|
73
|
|
|
self.assertTrue(os.path.exists(d)) |
|
74
|
|
|
return d |
|
75
|
|
|
|
|
76
|
|
|
@Timer.decorator |
|
77
|
|
|
def test_draw_img_percentage(self): |
|
78
|
|
|
"""Draw text onto an image based on percentage.""" |
|
79
|
|
|
with PillowImage(img=self.img_path) as draw: |
|
80
|
|
|
draw.draw_img(self.wtrmrk_path, opacity=0.08, rotate=30, x=.5, y=.1) |
|
81
|
|
|
d = draw.save(destination=self.result_dir, file_name='draw_img_percentage') |
|
82
|
|
|
|
|
83
|
|
|
# Assert file exists |
|
84
|
|
|
self.assertTrue(os.path.exists(d)) |
|
85
|
|
|
return d |
|
86
|
|
|
|
|
87
|
|
|
@Timer.decorator |
|
88
|
|
|
def test_draw_img_resized(self): |
|
89
|
|
|
"""Draw text onto a resized image.""" |
|
90
|
|
|
longest_side = 500 |
|
91
|
|
|
with PillowImage(img=self.img_path) as draw: |
|
92
|
|
|
draw.draw_img(self.wtrmrk_path, opacity=0.08, rotate=30) |
|
93
|
|
|
draw.resize(longest_side) |
|
94
|
|
|
d = draw.save(destination=self.result_dir, file_name='draw_img_resized') |
|
95
|
|
|
|
|
96
|
|
|
# Assert file exists |
|
97
|
|
|
self.assertTrue(os.path.exists(d)) |
|
98
|
|
|
|
|
99
|
|
|
# Assert actual longest edge is equal to target longest edge |
|
100
|
|
|
self.assertEqual(longest_side, draw.longest_side) |
|
101
|
|
|
return d |
|
102
|
|
|
|
|
103
|
|
|
@Timer.decorator |
|
104
|
|
|
def test_draw_img_resize_width(self): |
|
105
|
|
|
"""Draw text onto a horizontally resized image.""" |
|
106
|
|
|
width = 300 |
|
107
|
|
|
with PillowImage(img=self.img_path) as draw: |
|
108
|
|
|
draw.resize_width(width) |
|
109
|
|
|
d = draw.save(destination=self.result_dir, file_name='draw_img_resized_width') |
|
110
|
|
|
|
|
111
|
|
|
# Assert file exists |
|
112
|
|
|
self.assertTrue(os.path.exists(d)) |
|
113
|
|
|
|
|
114
|
|
|
# Assert actual longest edge is equal to target longest edge |
|
115
|
|
|
self.assertEqual(width, draw.width) |
|
116
|
|
|
return d |
|
117
|
|
|
|
|
118
|
|
|
@Timer.decorator |
|
119
|
|
|
def test_draw_img_resize_height(self): |
|
120
|
|
|
"""Draw text onto a vertically resized image.""" |
|
121
|
|
|
height = 300 |
|
122
|
|
|
with PillowImage(img=self.img_path) as draw: |
|
123
|
|
|
draw.resize_height(height) |
|
124
|
|
|
d = draw.save(destination=self.result_dir, file_name='draw_img_resized_height') |
|
125
|
|
|
|
|
126
|
|
|
# Assert file exists |
|
127
|
|
|
self.assertTrue(os.path.exists(d)) |
|
128
|
|
|
|
|
129
|
|
|
# Assert actual longest edge is equal to target longest edge |
|
130
|
|
|
self.assertEqual(height, draw.height) |
|
131
|
|
|
return d |
|
132
|
|
|
|
|
133
|
|
|
@Timer.decorator |
|
134
|
|
|
def test_rotate(self): |
|
135
|
|
|
"""Draw a rotated image.""" |
|
136
|
|
|
with PillowImage() as draw: |
|
137
|
|
|
draw.draw_img(self.img_path) |
|
138
|
|
|
draw.rotate(30) |
|
139
|
|
|
d = draw.save(destination=self.result_dir, file_name='rotate') |
|
140
|
|
|
|
|
141
|
|
|
# Assert file exists |
|
142
|
|
|
self.assertTrue(os.path.exists(d)) |
|
143
|
|
|
return d |
|
144
|
|
|
|
|
145
|
|
|
@Timer.decorator |
|
146
|
|
|
def test_size(self): |
|
147
|
|
|
"""Draw text onto an image with a modified size.""" |
|
148
|
|
|
with PillowImage(img=self.img_path) as draw: |
|
149
|
|
|
size = draw.size |
|
150
|
|
|
d = draw.save(destination=self.result_dir, file_name='size') |
|
151
|
|
|
|
|
152
|
|
|
# Assert file exists |
|
153
|
|
|
self.assertTrue(os.path.exists(d)) |
|
154
|
|
|
|
|
155
|
|
|
# Assert image size is correct |
|
156
|
|
|
self.assertIsInstance(size, tuple) |
|
157
|
|
|
self.assertTrue(size == (4094, 1317)) |
|
158
|
|
|
return d |
|
159
|
|
|
|
|
160
|
|
|
@Timer.decorator |
|
161
|
|
|
def test_width(self): |
|
162
|
|
|
"""Test the width of an image.""" |
|
163
|
|
|
with PillowImage(img=self.img_path) as draw: |
|
164
|
|
|
width = draw.width |
|
165
|
|
|
d = draw.save(destination=self.result_dir, file_name='width') |
|
166
|
|
|
|
|
167
|
|
|
# Assert file exists |
|
168
|
|
|
self.assertTrue(os.path.exists(d)) |
|
169
|
|
|
|
|
170
|
|
|
# Assert image size is correct |
|
171
|
|
|
self.assertTrue(width == 4094) |
|
172
|
|
|
return d |
|
173
|
|
|
|
|
174
|
|
|
@Timer.decorator |
|
175
|
|
|
def test_height(self): |
|
176
|
|
|
"""Test the height of an image.""" |
|
177
|
|
|
with PillowImage(img=self.img_path) as draw: |
|
178
|
|
|
height = draw.height |
|
179
|
|
|
d = draw.save(destination=self.result_dir, file_name='height') |
|
180
|
|
|
|
|
181
|
|
|
# Assert file exists |
|
182
|
|
|
self.assertTrue(os.path.exists(d)) |
|
183
|
|
|
|
|
184
|
|
|
# Assert image size is correct |
|
185
|
|
|
self.assertTrue(height == 1317) |
|
186
|
|
|
return d |
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
|
if __name__ == '__main__': |
|
190
|
|
|
unittest.main() |
|
191
|
|
|
|