Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(414)

Side by Side Diff: chrome/test/functional/ispy/image_tools_test.py

Issue 16855010: Python Tools for Pixel-by-Pixel Image Comparison (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: minor changes: math.ceiling to prevent rounding errors, [0:3] subsections of lists. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
craigdh 2013/06/14 19:08:40 just import PIL and use PIL.Image
cgrimm 2013/06/17 16:24:58 The __init__.py file in PIL isn't configured to im
7 import image_tools
8
9
10 def _GenImage(size, color):
11 return Image.new('RGB', size, color)
12
13
14 def _AllPixelsOfColor(image, color):
15 data = image.getdata()
craigdh 2013/06/14 19:08:40 no need for the intermediate variable
cgrimm 2013/06/17 16:24:58 Done.
16 return not any(px != color for px in data)
17
18
19 class ImageToolsTest(unittest.TestCase):
20
21 def testAreTheSameSize(self):
22 black25x25 = _GenImage((25, 25), (0, 0, 0))
23 white25x25 = _GenImage((25, 25), (255, 255, 255))
24 black50x50 = _GenImage((50, 50), (0, 0, 0))
25 white50x50 = _GenImage((50, 50), (255, 255, 255))
26
27 self.assertTrue(image_tools._AreTheSameSize([black25x25, black25x25]))
28 self.assertTrue(image_tools._AreTheSameSize([white25x25, white25x25]))
29 self.assertTrue(image_tools._AreTheSameSize([black50x50, black50x50]))
30 self.assertTrue(image_tools._AreTheSameSize([white50x50, white50x50]))
31 self.assertTrue(image_tools._AreTheSameSize([black25x25, white25x25]))
32 self.assertTrue(image_tools._AreTheSameSize([black50x50, white50x50]))
33
34 self.assertFalse(image_tools._AreTheSameSize([black50x50, black25x25]))
35 self.assertFalse(image_tools._AreTheSameSize([white50x50, white25x25]))
36 self.assertFalse(image_tools._AreTheSameSize([black25x25, white50x50]))
37 self.assertFalse(image_tools._AreTheSameSize([black50x50, white25x25]))
38
39 self.assertRaises(Exception, image_tools._AreTheSameSize, [])
40 self.assertRaises(Exception, image_tools._AreTheSameSize, [black50x50])
41
42 def testGetColorDist(self):
43 px1 = (0, 0, 0)
44 px2 = (255, 255, 255)
45 px3 = (0, 255, 255)
46 px4 = (0, 0, 255)
47
48 self.assertAlmostEquals(image_tools._GetColorDist(px1, px1), 0., places=4)
49 self.assertAlmostEquals(image_tools._GetColorDist(px1, px2), 1., places=4)
50 self.assertAlmostEquals(image_tools._GetColorDist(px2, px2), 0., places=4)
51 self.assertAlmostEquals(image_tools._GetColorDist(px1, px3), 2./3, places=4)
52 self.assertAlmostEquals(image_tools._GetColorDist(px1, px4), 1./3, places=4)
53 self.assertAlmostEquals(image_tools._GetColorDist(px3, px4), 1./3, places=4)
54
55 def testGetColorDistAsColor(self):
56 px1 = (0, 0, 0)
57 px2 = (255, 255, 255)
58
59 self.assertEquals(image_tools._GetColorDistAsColor(px1, px2),
60 (255, 255, 255))
61 self.assertEquals(image_tools._GetColorDistAsColor(px1, px1),
62 (0, 0, 0))
63 self.assertEquals(image_tools._GetColorDistAsColor(px2, px2),
64 (0, 0, 0))
65
66 def testBrightness(self):
67 px1 = (0, 0, 0)
68 px2 = (255, 255, 255)
69 px3 = (1, 2, 3)
70
71 self.assertEquals(image_tools._Brightness(px1), 0)
72 self.assertEquals(image_tools._Brightness(px2), 255*3)
73 self.assertEquals(image_tools._Brightness(px3), 6)
74
75 def testMinPixel(self):
76 px1 = (0, 0, 0)
77 px2 = (255, 255, 255)
78 px3 = (1, 2, 3)
79
80 self.assertEquals(image_tools._MinPixel(px1, px2), px1)
81 self.assertEquals(image_tools._MinPixel(px1, px3), px1)
82 self.assertEquals(image_tools._MinPixel(px2, px3), px3)
83 self.assertEquals(image_tools._MinPixel(px1, px1), px1)
84
85 def testMaxPixel(self):
86 px1 = (0, 0, 0)
87 px2 = (255, 255, 255)
88 px3 = (1, 2, 3)
89
90 self.assertEquals(image_tools._MaxPixel(px1, px2), px2)
91 self.assertEquals(image_tools._MaxPixel(px1, px3), px3)
92 self.assertEquals(image_tools._MaxPixel(px2, px3), px2)
93 self.assertEquals(image_tools._MaxPixel(px1, px1), px1)
94
95 def testCreateMask(self):
96 black25x25 = _GenImage((25, 25), (0, 0, 0))
97 black50x50 = _GenImage((50, 50), (0, 0, 0))
98 white25x25 = _GenImage((25, 25), (255, 255, 255))
99 gray25x25 = _GenImage((25, 25), (127, 127, 127))
100
101 m1 = image_tools.CreateMask([black25x25, white25x25], True, 1)
102 self.assertTrue(_AllPixelsOfColor(m1, (255, 255, 255)))
103 m2 = image_tools.CreateMask([black25x25, black25x25], True, 1)
104 self.assertTrue(_AllPixelsOfColor(m2, (0, 0, 0)))
105 m3 = image_tools.CreateMask([white25x25, white25x25], True, 1)
106 self.assertTrue(_AllPixelsOfColor(m3, (0, 0, 0)))
107
108 m4 = image_tools.CreateMask([white25x25, gray25x25], False, 0)
109 self.assertFalse(_AllPixelsOfColor(m4, (0, 0, 0)) or
110 _AllPixelsOfColor(m4, (255, 255, 255)))
111
112 m5 = image_tools.CreateMask([white25x25, white25x25], True, -1)
113 self.assertTrue(_AllPixelsOfColor(m5, (255, 255, 255)))
114 m6 = image_tools.CreateMask([white25x25, black25x25], True, -1)
115 self.assertTrue(_AllPixelsOfColor(m6, (255, 255, 255)))
116
117 self.assertRaises(Exception, image_tools.CreateMask,
118 [white25x25, black50x50], True, 1)
119
120 def testComputeLargestDifference(self):
121 white25x25 = _GenImage((25, 25), (255, 255, 255))
122 black25x25 = _GenImage((25, 25), (0, 0, 0))
123 wd = white25x25.getdata()
124 bd = black25x25.getdata()
125
126 self.assertEquals(list(image_tools._ComputeLargestDifference(wd, bd, bd)),
127 list(wd))
128 self.assertEquals(list(image_tools._ComputeLargestDifference(bd, wd, bd)),
129 list(wd))
130 self.assertEquals(list(image_tools._ComputeLargestDifference(wd, wd, wd)),
131 list(wd))
132 self.assertEquals(list(image_tools._ComputeLargestDifference(bd, bd, bd)),
133 list(bd))
134 self.assertEquals(list(image_tools._ComputeLargestDifference(bd, wd, wd)),
135 list(bd))
136
137 def testThresholdColorDiff(self):
138 p1 = ((0, 0, 0), (1, 2, 3))
139 p2 = ((0, 0, 0), (0, 0, 0))
140
141 self.assertEquals(image_tools._ThresholdColorDiff(p1[0], p1[1], 0), 1.)
142 self.assertEquals(image_tools._ThresholdColorDiff(p2[0], p2[1], 0), 0.)
143 self.assertEquals(image_tools._ThresholdColorDiff(p1[0], p1[1],
144 14./(3*255**2)), 0.)
145 self.assertEquals(image_tools._ThresholdColorDiff(p1[0], p1[1],
146 13./(3*255**2)), 1.)
147
148 def testMaskToValue(self):
149 px1 = (0, 0, 0)
150 px2 = (255, 255, 255)
151 px3 = (100, 100, 100)
152
153 self.assertEquals(image_tools._MaskToValue(px1), 1.)
154 self.assertEquals(image_tools._MaskToValue(px2), 0.)
155 self.assertRaises(Exception, image_tools._MaskToValue, px3)
156
157 def testTotalDifferentPixels(self):
158 white25x25 = _GenImage((25, 25), (255, 255, 255))
159 white50x50 = _GenImage((50, 50), (255, 255, 255))
160 black25x25 = _GenImage((25, 25), (0, 0, 0))
161
162 self.assertEquals(image_tools.TotalDifferentPixels(white25x25,
163 white25x25),
164 0)
165 self.assertEquals(image_tools.TotalDifferentPixels(black25x25,
166 black25x25),
167 0)
168 self.assertEquals(image_tools.TotalDifferentPixels(white25x25,
169 black25x25),
170 25*25)
171 self.assertEquals(image_tools.TotalDifferentPixels(white25x25,
172 black25x25,
173 mask=white25x25),
174 0)
175 self.assertEquals(image_tools.TotalDifferentPixels(white25x25,
176 white25x25,
177 mask=white25x25),
178 0)
179 self.assertEquals(image_tools.TotalDifferentPixels(white25x25,
180 black25x25,
181 mask=black25x25),
182 25*25)
183 self.assertEquals(image_tools.TotalDifferentPixels(white25x25,
184 white25x25,
185 mask=black25x25),
186 0)
187 self.assertRaises(Exception, image_tools.TotalDifferentPixels,
188 white25x25, white50x50)
189 self.assertRaises(Exception, image_tools.TotalDifferentPixels,
190 white25x25, white25x25, mask=white50x50)
191
192 def testSameImage(self):
193 white25x25 = _GenImage((25, 25), (255, 255, 255))
194 black25x25 = _GenImage((25, 25), (0, 0, 0))
195 white50x50 = _GenImage((50, 50), (255, 255, 255))
196
197 self.assertTrue(image_tools.SameImage(white25x25, white25x25,
198 max_different_pixels=0))
199 self.assertTrue(image_tools.SameImage(white25x25, black25x25,
200 max_different_pixels=25*25))
201 self.assertFalse(image_tools.SameImage(black25x25, black25x25,
202 max_different_pixels=-1))
203
204 self.assertTrue(image_tools.SameImage(white25x25, black25x25,
205 max_different_pixels=25*25,
206 mask=white25x25))
207 self.assertTrue(image_tools.SameImage(white25x25, white25x25,
208 max_different_pixels=0,
209 mask=white25x25))
210 self.assertFalse(image_tools.SameImage(white25x25, white25x25,
211 max_different_pixels=-1,
212 mask=white25x25))
213
214 self.assertRaises(Exception, image_tools.SameImage,
215 white25x25, white50x50,
216 max_different_pixels=1)
217 self.assertRaises(Exception, image_tools.SameImage,
218 white25x25, white25x25,
219 max_different_pixels=1, mask=white50x50)
220
221 def _GenImage(self, size, color):
222 return Image.new('RGB', size, color)
223
224 def _AllPixelsOfColor(self, image, color):
225 data = image.getdata()
226 for i in data:
craigdh 2013/06/14 19:08:40 just return not any(color != px for px in image.ge
cgrimm 2013/06/17 16:24:58 Done.
227 if i == color:
228 continue
229 else:
230 return False
231 return True
232
craigdh 2013/06/14 19:08:40 two blank lines here
cgrimm 2013/06/17 16:24:58 Done.
233 if __name__ == '__main__':
234 unittest.main()
235
236
OLDNEW
« chrome/test/functional/ispy/image_tools.py ('K') | « chrome/test/functional/ispy/image_tools.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698