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

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: 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 from google3.pyglib import flags
craigdh 2013/06/13 19:28:31 Chrome is open source. You can't use google3. Inst
6 from google3.testing.pybase import googletest
7 from PIL import Image
8 import image_tools
9
10
11 class ImageToolsTest(googletest.TestCase):
12
13 def testAreTheSameSize(self):
14 black25x25 = self._GenImage((25, 25), (0, 0, 0))
15 white25x25 = self._GenImage((25, 25), (255, 255, 255))
16 black50x50 = self._GenImage((50, 50), (0, 0, 0))
17 white50x50 = self._GenImage((50, 50), (255, 255, 255))
18
19 self.assertTrue(image_tools.AreTheSameSize(black25x25, black25x25))
20 self.assertTrue(image_tools.AreTheSameSize(white25x25, white25x25))
21 self.assertTrue(image_tools.AreTheSameSize(black50x50, black50x50))
22 self.assertTrue(image_tools.AreTheSameSize(white50x50, white50x50))
23 self.assertTrue(image_tools.AreTheSameSize(black25x25, white25x25))
24 self.assertTrue(image_tools.AreTheSameSize(black50x50, white50x50))
25
26 self.assertFalse(image_tools.AreTheSameSize(black50x50, black25x25))
27 self.assertFalse(image_tools.AreTheSameSize(white50x50, white25x25))
28 self.assertFalse(image_tools.AreTheSameSize(black25x25, white50x50))
29 self.assertFalse(image_tools.AreTheSameSize(black50x50, white25x25))
30
31 self.assertRaises(image_tools.Error, image_tools.AreTheSameSize)
32 self.assertRaises(image_tools.Error, image_tools.AreTheSameSize, black50x50)
33
34 def testGetColorDist(self):
35 px1 = (0, 0, 0)
36 px2 = (255, 255, 255)
37 px3 = (0, 255, 255)
38 px4 = (0, 0, 255)
39
40 self.assertAlmostEquals(image_tools.GetColorDist(px1, px1), 0., places=4)
41 self.assertAlmostEquals(image_tools.GetColorDist(px1, px2), 1., places=4)
42 self.assertAlmostEquals(image_tools.GetColorDist(px2, px2), 0., places=4)
43 self.assertAlmostEquals(image_tools.GetColorDist(px1, px3), 2./3, places=4)
44 self.assertAlmostEquals(image_tools.GetColorDist(px1, px4), 1./3, places=4)
45 self.assertAlmostEquals(image_tools.GetColorDist(px3, px4), 1./3, places=4)
46
47 def testGetColorDistAsColor(self):
48 px1 = (0, 0, 0)
49 px2 = (255, 255, 255)
50
51 self.assertEquals(image_tools.GetColorDistAsColor(px1, px2),
52 (255, 255, 255))
53 self.assertEquals(image_tools.GetColorDistAsColor(px1, px1),
54 (0, 0, 0))
55 self.assertEquals(image_tools.GetColorDistAsColor(px2, px2),
56 (0, 0, 0))
57
58 def testBrightness(self):
59 px1 = (0, 0, 0)
60 px2 = (255, 255, 255)
61 px3 = (1, 2, 3)
62
63 self.assertEquals(image_tools.Brightness(px1), 0)
64 self.assertEquals(image_tools.Brightness(px2), 255*3)
65 self.assertEquals(image_tools.Brightness(px3), 6)
66
67 def testMinPixel(self):
68 px1 = (0, 0, 0)
69 px2 = (255, 255, 255)
70 px3 = (1, 2, 3)
71
72 self.assertEquals(image_tools.MinPixel(px1, px2), px1)
73 self.assertEquals(image_tools.MinPixel(px1, px3), px1)
74 self.assertEquals(image_tools.MinPixel(px2, px3), px3)
75 self.assertEquals(image_tools.MinPixel(px1, px1), px1)
76
77 def testMaxPixel(self):
78 px1 = (0, 0, 0)
79 px2 = (255, 255, 255)
80 px3 = (1, 2, 3)
81
82 self.assertEquals(image_tools.MaxPixel(px1, px2), px2)
83 self.assertEquals(image_tools.MaxPixel(px1, px3), px3)
84 self.assertEquals(image_tools.MaxPixel(px2, px3), px2)
85 self.assertEquals(image_tools.MaxPixel(px1, px1), px1)
86
87 def testCreateMask(self):
88 black25x25 = self._GenImage((25, 25), (0, 0, 0))
89 black50x50 = self._GenImage((50, 50), (0, 0, 0))
90 white25x25 = self._GenImage((25, 25), (255, 255, 255))
91 gray25x25 = self._GenImage((25, 25), (127, 127, 127))
92
93 m1 = image_tools.CreateMask(True, 1, black25x25, white25x25)
94 self.assertTrue(self._AllPixelsOfColor(m1, (255, 255, 255)))
95 m2 = image_tools.CreateMask(True, 1, black25x25, black25x25)
96 self.assertTrue(self._AllPixelsOfColor(m2, (0, 0, 0)))
97 m3 = image_tools.CreateMask(True, 1, white25x25, white25x25)
98 self.assertTrue(self._AllPixelsOfColor(m3, (0, 0, 0)))
99
100 m4 = image_tools.CreateMask(False, 0, white25x25, gray25x25)
101 self.assertFalse(self._AllPixelsOfColor(m4, (0, 0, 0)) or
102 self._AllPixelsOfColor(m4, (255, 255, 255)))
103
104 m5 = image_tools.CreateMask(True, -1, white25x25, white25x25)
105 self.assertTrue(self._AllPixelsOfColor(m5, (255, 255, 255)))
106 m6 = image_tools.CreateMask(True, -1, white25x25, black25x25)
107 self.assertTrue(self._AllPixelsOfColor(m6, (255, 255, 255)))
108
109 self.assertRaises(image_tools.Error, image_tools.CreateMask,
110 True, 1, white25x25, black50x50)
111
112 def testComputeLargestDifference(self):
113 white25x25 = self._GenImage((25, 25), (255, 255, 255))
114 black25x25 = self._GenImage((25, 25), (0, 0, 0))
115 wd = list(white25x25.getdata())
116 bd = list(black25x25.getdata())
117
118 self.assertEquals(image_tools.ComputeLargestDifference(wd, bd, bd), wd)
119 self.assertEquals(image_tools.ComputeLargestDifference(bd, wd, bd), wd)
120 self.assertEquals(image_tools.ComputeLargestDifference(wd, wd, wd), wd)
121 self.assertEquals(image_tools.ComputeLargestDifference(bd, bd, bd), bd)
122 self.assertEquals(image_tools.ComputeLargestDifference(bd, wd, wd), bd)
123
124 def testThresholdColorDiff(self):
125 p1 = ((0, 0, 0), (1, 2, 3))
126 p2 = ((0, 0, 0), (0, 0, 0))
127
128 self.assertEquals(image_tools.ThresholdColorDiff(p1[0], p1[1], 0), 1.)
129 self.assertEquals(image_tools.ThresholdColorDiff(p2[0], p2[1], 0), 0.)
130 self.assertEquals(image_tools.ThresholdColorDiff(p1[0], p1[1],
131 14./(3*255**2)), 0.)
132 self.assertEquals(image_tools.ThresholdColorDiff(p1[0], p1[1],
133 13./(3*255**2)), 1.)
134
135 def testMaskToValue(self):
136 px1 = (0, 0, 0)
137 px2 = (255, 255, 255)
138 px3 = (100, 100, 100)
139
140 self.assertEquals(image_tools.MaskToValue(px1), 1.)
141 self.assertEquals(image_tools.MaskToValue(px2), 0.)
142 self.assertRaises(image_tools.Error, image_tools.MaskToValue, px3)
143
144 def testSimilarity(self):
145 white25x25 = self._GenImage((25, 25), (255, 255, 255))
146 white50x50 = self._GenImage((50, 50), (255, 255, 255))
147 black25x25 = self._GenImage((25, 25), (0, 0, 0))
148
149 self.assertEquals(image_tools.Similarity(white25x25, white25x25), 1.)
150 self.assertEquals(image_tools.Similarity(black25x25, black25x25), 1.)
151 self.assertEquals(image_tools.Similarity(white25x25, black25x25), 0.)
152 self.assertEquals(image_tools.Similarity(white25x25,
153 black25x25,
154 mask=white25x25),
155 1.)
156 self.assertEquals(image_tools.Similarity(white25x25,
157 white25x25,
158 mask=white25x25),
159 1.)
160 self.assertEquals(image_tools.Similarity(white25x25,
161 black25x25,
162 mask=black25x25),
163 0.)
164 self.assertEquals(image_tools.Similarity(white25x25,
165 white25x25,
166 mask=black25x25),
167 1.)
168 self.assertRaises(image_tools.Error, image_tools.Similarity,
169 white25x25, white50x50)
170 self.assertRaises(image_tools.Error, image_tools.Similarity,
171 white25x25, white25x25, mask=white50x50)
172
173 def testSameImage(self):
174 white25x25 = self._GenImage((25, 25), (255, 255, 255))
175 black25x25 = self._GenImage((25, 25), (0, 0, 0))
176 white50x50 = self._GenImage((50, 50), (255, 255, 255))
177
178 self.assertTrue(image_tools.SameImage(white25x25, white25x25, 0.97))
179 self.assertTrue(image_tools.SameImage(white25x25, black25x25, 0.))
180 self.assertFalse(image_tools.SameImage(black25x25, black25x25, 1.01))
181
182 self.assertTrue(image_tools.SameImage(white25x25, black25x25,
183 0., mask=white25x25))
184 self.assertTrue(image_tools.SameImage(white25x25, white25x25,
185 1., mask=white25x25))
186 self.assertFalse(image_tools.SameImage(white25x25, white25x25,
187 1.01, mask=white25x25))
188
189 self.assertRaises(image_tools.Error, image_tools.SameImage,
190 white25x25, white50x50, 1.)
191 self.assertRaises(image_tools.Error, image_tools.SameImage,
192 white25x25, white25x25, 1., mask=white50x50)
193
194 def _GenImage(self, size, color):
craigdh 2013/06/13 19:28:31 This can be a module level function.
195 return Image.new('RGB', size, color)
196
197 def _AllPixelsOfColor(self, image, color):
craigdh 2013/06/13 19:28:31 ditto
198 data = image.getdata()
199 for i in data:
craigdh 2013/06/13 19:28:31 Just use any()
200 if i == color:
201 continue
202 else:
203 return False
204 return True
205
206 if __name__ == '__main__':
207 googletest.main()
208
209
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