OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 """ | 3 """ |
4 Copyright 2013 Google Inc. | 4 Copyright 2013 Google Inc. |
5 | 5 |
6 Use of this source code is governed by a BSD-style license that can be | 6 Use of this source code is governed by a BSD-style license that can be |
7 found in the LICENSE file. | 7 found in the LICENSE file. |
8 | 8 |
9 Calulate differences between image pairs, and store them in a database. | 9 Calulate differences between image pairs, and store them in a database. |
10 """ | 10 """ |
11 | 11 |
12 import contextlib | 12 import contextlib |
| 13 import csv |
13 import logging | 14 import logging |
14 import os | 15 import os |
15 import re | 16 import re |
16 import shutil | 17 import shutil |
| 18 import sys |
| 19 import tempfile |
17 import urllib | 20 import urllib |
18 try: | 21 try: |
19 from PIL import Image, ImageChops | 22 from PIL import Image, ImageChops |
20 except ImportError: | 23 except ImportError: |
21 raise ImportError('Requires PIL to be installed; see ' | 24 raise ImportError('Requires PIL to be installed; see ' |
22 + 'http://www.pythonware.com/products/pil/') | 25 + 'http://www.pythonware.com/products/pil/') |
23 | 26 |
| 27 # Set the PYTHONPATH to include the tools directory. |
| 28 sys.path.append( |
| 29 os.path.join( |
| 30 os.path.dirname(os.path.realpath(__file__)), os.pardir, os.pardir, |
| 31 'tools')) |
| 32 import find_run_binary |
| 33 |
| 34 SKPDIFF_BINARY_NAME = 'skpdiff' |
| 35 |
24 DEFAULT_IMAGE_SUFFIX = '.png' | 36 DEFAULT_IMAGE_SUFFIX = '.png' |
25 DEFAULT_IMAGES_SUBDIR = 'images' | 37 DEFAULT_IMAGES_SUBDIR = 'images' |
26 | 38 |
27 DISALLOWED_FILEPATH_CHAR_REGEX = re.compile('[^\w\-]') | 39 DISALLOWED_FILEPATH_CHAR_REGEX = re.compile('[^\w\-]') |
28 | 40 |
29 DIFFS_SUBDIR = 'diffs' | 41 DIFFS_SUBDIR = 'diffs' |
30 WHITEDIFFS_SUBDIR = 'whitediffs' | 42 WHITEDIFFS_SUBDIR = 'whitediffs' |
31 | 43 |
32 VALUES_PER_BAND = 256 | 44 VALUES_PER_BAND = 256 |
33 | 45 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 # Generate the whitediff image (any differing pixels show as white). | 104 # Generate the whitediff image (any differing pixels show as white). |
93 # This is tricky, because when you convert color images to grayscale or | 105 # This is tricky, because when you convert color images to grayscale or |
94 # black & white in PIL, it has its own ideas about thresholds. | 106 # black & white in PIL, it has its own ideas about thresholds. |
95 # We have to force it: if a pixel has any color at all, it's a '1'. | 107 # We have to force it: if a pixel has any color at all, it's a '1'. |
96 bands = diff_image.split() | 108 bands = diff_image.split() |
97 graydiff_image = ImageChops.lighter(ImageChops.lighter( | 109 graydiff_image = ImageChops.lighter(ImageChops.lighter( |
98 bands[0], bands[1]), bands[2]) | 110 bands[0], bands[1]), bands[2]) |
99 whitediff_image = (graydiff_image.point(lambda p: p > 0 and VALUES_PER_BAND) | 111 whitediff_image = (graydiff_image.point(lambda p: p > 0 and VALUES_PER_BAND) |
100 .convert('1', dither=Image.NONE)) | 112 .convert('1', dither=Image.NONE)) |
101 | 113 |
| 114 # Calculate the perceptual difference percentage. |
| 115 skpdiff_csv_dir = tempfile.mkdtemp() |
| 116 try: |
| 117 skpdiff_csv_output = os.path.join(skpdiff_csv_dir, 'skpdiff-output.csv') |
| 118 skpdiff_binary = find_run_binary.find_path_to_program(SKPDIFF_BINARY_NAME) |
| 119 expected_img = os.path.join(storage_root, expected_images_subdir, |
| 120 str(expected_image_locator) + image_suffix) |
| 121 actual_img = os.path.join(storage_root, actual_images_subdir, |
| 122 str(actual_image_locator) + image_suffix) |
| 123 find_run_binary.run_command( |
| 124 [skpdiff_binary, '-p', expected_img, actual_img, |
| 125 '--csv', skpdiff_csv_output, '-d', 'perceptual']) |
| 126 with contextlib.closing(open(skpdiff_csv_output)) as csv_file: |
| 127 for row in csv.DictReader(csv_file): |
| 128 perceptual_similarity = float(row[' perceptual'].strip()) |
| 129 if not 0 <= perceptual_similarity <= 1: |
| 130 # skpdiff outputs -1 if the images are different sizes. Treat any |
| 131 # output that does not lie in [0, 1] as having 0% perceptual |
| 132 # similarity. |
| 133 perceptual_similarity = 0 |
| 134 # skpdiff returns the perceptual similarity, convert it to get the |
| 135 # perceptual difference percentage. |
| 136 self._perceptual_difference = 100 - (perceptual_similarity * 100) |
| 137 finally: |
| 138 shutil.rmtree(skpdiff_csv_dir) |
| 139 |
102 # Final touches on diff_image: use whitediff_image as an alpha mask. | 140 # Final touches on diff_image: use whitediff_image as an alpha mask. |
103 # Unchanged pixels are transparent; differing pixels are opaque. | 141 # Unchanged pixels are transparent; differing pixels are opaque. |
104 diff_image.putalpha(whitediff_image) | 142 diff_image.putalpha(whitediff_image) |
105 | 143 |
106 # Store the diff and whitediff images generated above. | 144 # Store the diff and whitediff images generated above. |
107 diff_image_locator = _get_difference_locator( | 145 diff_image_locator = _get_difference_locator( |
108 expected_image_locator=expected_image_locator, | 146 expected_image_locator=expected_image_locator, |
109 actual_image_locator=actual_image_locator) | 147 actual_image_locator=actual_image_locator) |
110 basename = str(diff_image_locator) + image_suffix | 148 basename = str(diff_image_locator) + image_suffix |
111 _save_image(diff_image, os.path.join( | 149 _save_image(diff_image, os.path.join( |
112 storage_root, DIFFS_SUBDIR, basename)) | 150 storage_root, DIFFS_SUBDIR, basename)) |
113 _save_image(whitediff_image, os.path.join( | 151 _save_image(whitediff_image, os.path.join( |
114 storage_root, WHITEDIFFS_SUBDIR, basename)) | 152 storage_root, WHITEDIFFS_SUBDIR, basename)) |
115 | 153 |
116 # Calculate difference metrics. | 154 # Calculate difference metrics. |
117 (self._width, self._height) = diff_image.size | 155 (self._width, self._height) = diff_image.size |
118 self._num_pixels_differing = ( | 156 self._num_pixels_differing = ( |
119 whitediff_image.histogram()[VALUES_PER_BAND - 1]) | 157 whitediff_image.histogram()[VALUES_PER_BAND - 1]) |
120 | 158 |
121 def get_num_pixels_differing(self): | 159 def get_num_pixels_differing(self): |
122 """Returns the absolute number of pixels that differ.""" | 160 """Returns the absolute number of pixels that differ.""" |
123 return self._num_pixels_differing | 161 return self._num_pixels_differing |
124 | 162 |
125 def get_percent_pixels_differing(self): | 163 def get_percent_pixels_differing(self): |
126 """Returns the percentage of pixels that differ, as a float between | 164 """Returns the percentage of pixels that differ, as a float between |
127 0 and 100 (inclusive).""" | 165 0 and 100 (inclusive).""" |
128 return ((float(self._num_pixels_differing) * 100) / | 166 return ((float(self._num_pixels_differing) * 100) / |
129 (self._width * self._height)) | 167 (self._width * self._height)) |
130 | 168 |
| 169 def get_perceptual_difference(self): |
| 170 """Returns the perceptual difference percentage.""" |
| 171 return self._perceptual_difference |
| 172 |
131 def get_weighted_diff_measure(self): | 173 def get_weighted_diff_measure(self): |
132 """Returns a weighted measure of image diffs, as a float between 0 and 100 | 174 """Returns a weighted measure of image diffs, as a float between 0 and 100 |
133 (inclusive).""" | 175 (inclusive).""" |
134 return self._weighted_diff_measure | 176 return self._weighted_diff_measure |
135 | 177 |
136 def get_max_diff_per_channel(self): | 178 def get_max_diff_per_channel(self): |
137 """Returns the maximum difference between the expected and actual images | 179 """Returns the maximum difference between the expected and actual images |
138 for each R/G/B channel, as a list.""" | 180 for each R/G/B channel, as a list.""" |
139 return self._max_diff_per_channel | 181 return self._max_diff_per_channel |
140 | 182 |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 | 398 |
357 Args: | 399 Args: |
358 expected_image_locator: locator string pointing at expected image | 400 expected_image_locator: locator string pointing at expected image |
359 actual_image_locator: locator string pointing at actual image | 401 actual_image_locator: locator string pointing at actual image |
360 | 402 |
361 Returns: already-sanitized locator where the diffs between expected and | 403 Returns: already-sanitized locator where the diffs between expected and |
362 actual images can be found | 404 actual images can be found |
363 """ | 405 """ |
364 return "%s-vs-%s" % (_sanitize_locator(expected_image_locator), | 406 return "%s-vs-%s" % (_sanitize_locator(expected_image_locator), |
365 _sanitize_locator(actual_image_locator)) | 407 _sanitize_locator(actual_image_locator)) |
OLD | NEW |