OLD | NEW |
---|---|
(Empty) | |
1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import unittest | |
6 from PIL import Image | |
7 import image_tools | |
frankf
2013/06/18 01:52:18
Separate sytem libraries from user-defined librari
cgrimm
2013/06/18 18:53:02
Done.
| |
8 | |
9 | |
10 def _GenImage(size, color): | |
11 return Image.new('RGB', size, color) | |
12 | |
13 | |
14 def _AllPixelsOfColor(image, color): | |
15 return not any(px != color for px in image.getdata()) | |
16 | |
17 | |
18 class ImageToolsTest(unittest.TestCase): | |
19 | |
20 def testAreTheSameSize(self): | |
21 black25x25 = _GenImage((25, 25), (0, 0, 0)) | |
22 white25x25 = _GenImage((25, 25), (255, 255, 255)) | |
23 black50x50 = _GenImage((50, 50), (0, 0, 0)) | |
24 white50x50 = _GenImage((50, 50), (255, 255, 255)) | |
25 | |
26 self.assertTrue(image_tools._AreTheSameSize([black25x25, black25x25])) | |
27 self.assertTrue(image_tools._AreTheSameSize([white25x25, white25x25])) | |
28 self.assertTrue(image_tools._AreTheSameSize([black50x50, black50x50])) | |
29 self.assertTrue(image_tools._AreTheSameSize([white50x50, white50x50])) | |
30 self.assertTrue(image_tools._AreTheSameSize([black25x25, white25x25])) | |
31 self.assertTrue(image_tools._AreTheSameSize([black50x50, white50x50])) | |
32 | |
33 self.assertFalse(image_tools._AreTheSameSize([black50x50, black25x25])) | |
34 self.assertFalse(image_tools._AreTheSameSize([white50x50, white25x25])) | |
35 self.assertFalse(image_tools._AreTheSameSize([black25x25, white50x50])) | |
36 self.assertFalse(image_tools._AreTheSameSize([black50x50, white25x25])) | |
37 | |
38 self.assertRaises(Exception, image_tools._AreTheSameSize, []) | |
39 self.assertRaises(Exception, image_tools._AreTheSameSize, [black50x50]) | |
40 | |
41 def testGetColorDist(self): | |
42 px1 = (0, 0, 0) | |
43 px2 = (255, 255, 255) | |
44 px3 = (0, 255, 255) | |
45 px4 = (0, 0, 255) | |
46 | |
47 self.assertAlmostEquals(image_tools._GetColorDist(px1, px1), 0., places=4) | |
48 self.assertAlmostEquals(image_tools._GetColorDist(px1, px2), 1., places=4) | |
49 self.assertAlmostEquals(image_tools._GetColorDist(px2, px2), 0., places=4) | |
50 self.assertAlmostEquals(image_tools._GetColorDist(px1, px3), 2./3, places=4) | |
51 self.assertAlmostEquals(image_tools._GetColorDist(px1, px4), 1./3, places=4) | |
52 self.assertAlmostEquals(image_tools._GetColorDist(px3, px4), 1./3, places=4) | |
53 | |
54 def testGetColorDistAsColor(self): | |
55 px1 = (0, 0, 0) | |
56 px2 = (255, 255, 255) | |
frankf
2013/06/18 01:52:18
If you reusing these, put up the fixtures in a Set
cgrimm
2013/06/18 18:53:02
Done.
| |
57 | |
58 self.assertEquals(image_tools._GetColorDistAsColor(px1, px2), | |
59 (255, 255, 255)) | |
60 self.assertEquals(image_tools._GetColorDistAsColor(px1, px1), | |
61 (0, 0, 0)) | |
62 self.assertEquals(image_tools._GetColorDistAsColor(px2, px2), | |
63 (0, 0, 0)) | |
64 | |
65 def testBrightness(self): | |
66 px1 = (0, 0, 0) | |
67 px2 = (255, 255, 255) | |
68 px3 = (1, 2, 3) | |
69 | |
70 self.assertEquals(image_tools._Brightness(px1), 0) | |
71 self.assertEquals(image_tools._Brightness(px2), 255*3) | |
72 self.assertEquals(image_tools._Brightness(px3), 6) | |
73 | |
74 def testMinPixel(self): | |
75 px1 = (0, 0, 0) | |
76 px2 = (255, 255, 255) | |
77 px3 = (1, 2, 3) | |
78 | |
79 self.assertEquals(image_tools._MinPixel(px1, px2), px1) | |
80 self.assertEquals(image_tools._MinPixel(px1, px3), px1) | |
81 self.assertEquals(image_tools._MinPixel(px2, px3), px3) | |
82 self.assertEquals(image_tools._MinPixel(px1, px1), px1) | |
83 | |
84 def testMaxPixel(self): | |
85 px1 = (0, 0, 0) | |
86 px2 = (255, 255, 255) | |
87 px3 = (1, 2, 3) | |
88 | |
89 self.assertEquals(image_tools._MaxPixel(px1, px2), px2) | |
90 self.assertEquals(image_tools._MaxPixel(px1, px3), px3) | |
91 self.assertEquals(image_tools._MaxPixel(px2, px3), px2) | |
92 self.assertEquals(image_tools._MaxPixel(px1, px1), px1) | |
93 | |
94 def testCreateMask(self): | |
95 black25x25 = _GenImage((25, 25), (0, 0, 0)) | |
96 black50x50 = _GenImage((50, 50), (0, 0, 0)) | |
97 white25x25 = _GenImage((25, 25), (255, 255, 255)) | |
98 gray25x25 = _GenImage((25, 25), (127, 127, 127)) | |
99 | |
100 m1 = image_tools.CreateMask([black25x25, white25x25], True, 1) | |
101 self.assertTrue(_AllPixelsOfColor(m1, (255, 255, 255))) | |
102 m2 = image_tools.CreateMask([black25x25, black25x25], True, 1) | |
103 self.assertTrue(_AllPixelsOfColor(m2, (0, 0, 0))) | |
104 m3 = image_tools.CreateMask([white25x25, white25x25], True, 1) | |
105 self.assertTrue(_AllPixelsOfColor(m3, (0, 0, 0))) | |
106 | |
107 m4 = image_tools.CreateMask([white25x25, gray25x25], False, 0) | |
108 self.assertFalse(_AllPixelsOfColor(m4, (0, 0, 0)) or | |
109 _AllPixelsOfColor(m4, (255, 255, 255))) | |
110 | |
111 m5 = image_tools.CreateMask([white25x25, white25x25], True, -1) | |
112 self.assertTrue(_AllPixelsOfColor(m5, (255, 255, 255))) | |
113 m6 = image_tools.CreateMask([white25x25, black25x25], True, -1) | |
114 self.assertTrue(_AllPixelsOfColor(m6, (255, 255, 255))) | |
115 | |
116 self.assertRaises(Exception, image_tools.CreateMask, | |
117 [white25x25, black50x50], True, 1) | |
118 | |
119 def testComputeLargestDifference(self): | |
120 white25x25 = _GenImage((25, 25), (255, 255, 255)) | |
121 black25x25 = _GenImage((25, 25), (0, 0, 0)) | |
122 wd = white25x25.getdata() | |
123 bd = black25x25.getdata() | |
124 | |
125 self.assertEquals(list(image_tools._ComputeLargestDifference(wd, bd, bd)), | |
126 list(wd)) | |
127 self.assertEquals(list(image_tools._ComputeLargestDifference(bd, wd, bd)), | |
128 list(wd)) | |
129 self.assertEquals(list(image_tools._ComputeLargestDifference(wd, wd, wd)), | |
130 list(wd)) | |
131 self.assertEquals(list(image_tools._ComputeLargestDifference(bd, bd, bd)), | |
132 list(bd)) | |
133 self.assertEquals(list(image_tools._ComputeLargestDifference(bd, wd, wd)), | |
134 list(bd)) | |
135 | |
136 def testThresholdColorDiff(self): | |
137 p1 = ((0, 0, 0), (1, 2, 3)) | |
138 p2 = ((0, 0, 0), (0, 0, 0)) | |
139 | |
140 self.assertEquals(image_tools._ThresholdColorDiff(p1[0], p1[1], 0), 1.) | |
141 self.assertEquals(image_tools._ThresholdColorDiff(p2[0], p2[1], 0), 0.) | |
142 self.assertEquals(image_tools._ThresholdColorDiff(p1[0], p1[1], | |
143 14./(3*255**2)), 0.) | |
144 self.assertEquals(image_tools._ThresholdColorDiff(p1[0], p1[1], | |
145 13./(3*255**2)), 1.) | |
146 | |
147 def testMaskToValue(self): | |
148 px1 = (0, 0, 0) | |
149 px2 = (255, 255, 255) | |
150 px3 = (100, 100, 100) | |
151 | |
152 self.assertEquals(image_tools._MaskToValue(px1), 1.) | |
153 self.assertEquals(image_tools._MaskToValue(px2), 0.) | |
154 self.assertRaises(Exception, image_tools._MaskToValue, px3) | |
155 | |
156 def testTotalDifferentPixels(self): | |
157 white25x25 = _GenImage((25, 25), (255, 255, 255)) | |
158 white50x50 = _GenImage((50, 50), (255, 255, 255)) | |
159 black25x25 = _GenImage((25, 25), (0, 0, 0)) | |
160 | |
161 self.assertEquals(image_tools.TotalDifferentPixels(white25x25, | |
162 white25x25), | |
163 0) | |
164 self.assertEquals(image_tools.TotalDifferentPixels(black25x25, | |
165 black25x25), | |
166 0) | |
167 self.assertEquals(image_tools.TotalDifferentPixels(white25x25, | |
168 black25x25), | |
169 25*25) | |
170 self.assertEquals(image_tools.TotalDifferentPixels(white25x25, | |
171 black25x25, | |
172 mask=white25x25), | |
173 0) | |
174 self.assertEquals(image_tools.TotalDifferentPixels(white25x25, | |
175 white25x25, | |
176 mask=white25x25), | |
177 0) | |
178 self.assertEquals(image_tools.TotalDifferentPixels(white25x25, | |
179 black25x25, | |
180 mask=black25x25), | |
181 25*25) | |
182 self.assertEquals(image_tools.TotalDifferentPixels(white25x25, | |
183 white25x25, | |
184 mask=black25x25), | |
185 0) | |
186 self.assertRaises(Exception, image_tools.TotalDifferentPixels, | |
187 white25x25, white50x50) | |
188 self.assertRaises(Exception, image_tools.TotalDifferentPixels, | |
189 white25x25, white25x25, mask=white50x50) | |
190 | |
191 def testSameImage(self): | |
192 white25x25 = _GenImage((25, 25), (255, 255, 255)) | |
193 black25x25 = _GenImage((25, 25), (0, 0, 0)) | |
194 white50x50 = _GenImage((50, 50), (255, 255, 255)) | |
195 | |
196 self.assertTrue(image_tools.SameImage(white25x25, white25x25, | |
197 max_different_pixels=0)) | |
198 self.assertTrue(image_tools.SameImage(white25x25, black25x25, | |
199 max_different_pixels=25*25)) | |
200 self.assertFalse(image_tools.SameImage(black25x25, black25x25, | |
201 max_different_pixels=-1)) | |
202 | |
203 self.assertTrue(image_tools.SameImage(white25x25, black25x25, | |
204 max_different_pixels=25*25, | |
205 mask=white25x25)) | |
206 self.assertTrue(image_tools.SameImage(white25x25, white25x25, | |
207 max_different_pixels=0, | |
208 mask=white25x25)) | |
209 self.assertFalse(image_tools.SameImage(white25x25, white25x25, | |
210 max_different_pixels=-1, | |
211 mask=white25x25)) | |
212 | |
213 self.assertRaises(Exception, image_tools.SameImage, | |
214 white25x25, white50x50, | |
215 max_different_pixels=1) | |
216 self.assertRaises(Exception, image_tools.SameImage, | |
217 white25x25, white25x25, | |
218 max_different_pixels=1, mask=white50x50) | |
219 | |
220 def testVisualizeImageDifferences(self): | |
221 white25x25 = _GenImage((25, 25), (255, 255, 255)) | |
222 black25x25 = _GenImage((25, 25), (0, 0, 0)) | |
223 white50x50 = _GenImage((50, 50), (255, 255, 255)) | |
224 self.assertTrue(_AllPixelsOfColor(image_tools.VisualizeImageDifferences( | |
225 white25x25, black25x25), (255, 255, 255))) | |
226 self.assertTrue(_AllPixelsOfColor(image_tools.VisualizeImageDifferences( | |
227 white25x25, white25x25), (0, 0, 0))) | |
228 self.assertTrue(_AllPixelsOfColor(image_tools.VisualizeImageDifferences( | |
229 black25x25, black25x25), (0, 0, 0))) | |
230 self.assertTrue(_AllPixelsOfColor(image_tools.VisualizeImageDifferences( | |
231 white25x25, black25x25, mask=white25x25), (0, 0, 0))) | |
232 self.assertTrue(_AllPixelsOfColor(image_tools.VisualizeImageDifferences( | |
233 white25x25, black25x25, mask=black25x25), (255, 255, 255))) | |
234 self.assertTrue(_AllPixelsOfColor(image_tools.VisualizeImageDifferences( | |
235 white25x25, white25x25, mask=white25x25), (0, 0, 0))) | |
236 self.assertTrue(_AllPixelsOfColor(image_tools.VisualizeImageDifferences( | |
237 white25x25, white25x25, mask=black25x25), (0, 0, 0))) | |
238 self.assertTrue(_AllPixelsOfColor(image_tools.VisualizeImageDifferences( | |
239 black25x25, black25x25, mask=black25x25), (0, 0, 0))) | |
240 self.assertTrue(_AllPixelsOfColor(image_tools.VisualizeImageDifferences( | |
241 black25x25, black25x25, mask=white25x25), (0, 0, 0))) | |
242 | |
243 self.assertRaises(Exception, | |
244 image_tools.VisualizeImageDifferences, | |
245 white25x25, | |
246 white50x50) | |
247 self.assertRaises(Exception, | |
248 image_tools.VisualizeImageDifferences, | |
249 white25x25, | |
250 black25x25, | |
251 mask=white50x50) | |
252 | |
253 | |
254 if __name__ == '__main__': | |
255 unittest.main() | |
OLD | NEW |