blob: 8328543c935d77bbdc011701d85356c78fe9270f [file] [log] [blame]
epoger@google.com9dddf6f2013-11-08 16:25:25 +00001#!/usr/bin/python
2
3"""
4Copyright 2013 Google Inc.
5
6Use of this source code is governed by a BSD-style license that can be
7found in the LICENSE file.
8
9Calulate differences between image pairs, and store them in a database.
10"""
11
12import contextlib
commit-bot@chromium.org44546f82014-02-11 18:21:26 +000013import csv
epoger@google.com9dddf6f2013-11-08 16:25:25 +000014import logging
15import os
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000016import re
epoger@google.com9dddf6f2013-11-08 16:25:25 +000017import shutil
commit-bot@chromium.org44546f82014-02-11 18:21:26 +000018import sys
19import tempfile
epoger@google.com9dddf6f2013-11-08 16:25:25 +000020import urllib
21try:
22 from PIL import Image, ImageChops
23except ImportError:
24 raise ImportError('Requires PIL to be installed; see '
25 + 'http://www.pythonware.com/products/pil/')
26
commit-bot@chromium.org44546f82014-02-11 18:21:26 +000027# Set the PYTHONPATH to include the tools directory.
28sys.path.append(
29 os.path.join(
30 os.path.dirname(os.path.realpath(__file__)), os.pardir, os.pardir,
31 'tools'))
32import find_run_binary
33
commit-bot@chromium.org4d0f0082014-02-18 14:38:22 +000034SKPDIFF_BINARY = find_run_binary.find_path_to_program('skpdiff')
commit-bot@chromium.org44546f82014-02-11 18:21:26 +000035
rmistry@google.com5861e522013-12-21 19:07:40 +000036DEFAULT_IMAGE_SUFFIX = '.png'
37DEFAULT_IMAGES_SUBDIR = 'images'
epoger@google.com9dddf6f2013-11-08 16:25:25 +000038
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000039DISALLOWED_FILEPATH_CHAR_REGEX = re.compile('[^\w\-]')
40
epoger@google.com9dddf6f2013-11-08 16:25:25 +000041DIFFS_SUBDIR = 'diffs'
42WHITEDIFFS_SUBDIR = 'whitediffs'
43
epoger@google.com214a0242013-11-22 19:26:18 +000044VALUES_PER_BAND = 256
45
commit-bot@chromium.org16f41802014-02-26 19:05:20 +000046# Keys used within DiffRecord dictionary representations.
47# NOTE: Keep these in sync with static/constants.js
48KEY__DIFFERENCE_DATA__MAX_DIFF_PER_CHANNEL = 'maxDiffPerChannel'
49KEY__DIFFERENCE_DATA__NUM_DIFF_PIXELS = 'numDifferingPixels'
50KEY__DIFFERENCE_DATA__PERCENT_DIFF_PIXELS = 'percentDifferingPixels'
51KEY__DIFFERENCE_DATA__PERCEPTUAL_DIFF = 'perceptualDifference'
52KEY__DIFFERENCE_DATA__WEIGHTED_DIFF = 'weightedDiffMeasure'
53
epoger@google.com9dddf6f2013-11-08 16:25:25 +000054
55class DiffRecord(object):
56 """ Record of differences between two images. """
57
58 def __init__(self, storage_root,
59 expected_image_url, expected_image_locator,
rmistry@google.com5861e522013-12-21 19:07:40 +000060 actual_image_url, actual_image_locator,
61 expected_images_subdir=DEFAULT_IMAGES_SUBDIR,
62 actual_images_subdir=DEFAULT_IMAGES_SUBDIR,
63 image_suffix=DEFAULT_IMAGE_SUFFIX):
epoger@google.com9dddf6f2013-11-08 16:25:25 +000064 """Download this pair of images (unless we already have them on local disk),
65 and prepare a DiffRecord for them.
66
67 TODO(epoger): Make this asynchronously download images, rather than blocking
68 until the images have been downloaded and processed.
69
70 Args:
71 storage_root: root directory on local disk within which we store all
72 images
73 expected_image_url: file or HTTP url from which we will download the
74 expected image
75 expected_image_locator: a unique ID string under which we will store the
76 expected image within storage_root (probably including a checksum to
77 guarantee uniqueness)
78 actual_image_url: file or HTTP url from which we will download the
79 actual image
80 actual_image_locator: a unique ID string under which we will store the
81 actual image within storage_root (probably including a checksum to
82 guarantee uniqueness)
rmistry@google.com5861e522013-12-21 19:07:40 +000083 expected_images_subdir: the subdirectory expected images are stored in.
84 actual_images_subdir: the subdirectory actual images are stored in.
85 image_suffix: the suffix of images.
epoger@google.com9dddf6f2013-11-08 16:25:25 +000086 """
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +000087 expected_image_locator = _sanitize_locator(expected_image_locator)
88 actual_image_locator = _sanitize_locator(actual_image_locator)
89
epoger@google.com9dddf6f2013-11-08 16:25:25 +000090 # Download the expected/actual images, if we don't have them already.
rmistry@google.com5861e522013-12-21 19:07:40 +000091 # TODO(rmistry): Add a parameter that makes _download_and_open_image raise
92 # an exception if images are not found locally (instead of trying to
93 # download them).
commit-bot@chromium.org8cc39a62014-03-04 16:46:22 +000094 expected_image_file = os.path.join(
95 storage_root, expected_images_subdir,
96 str(expected_image_locator) + image_suffix)
97 actual_image_file = os.path.join(
98 storage_root, actual_images_subdir,
99 str(actual_image_locator) + image_suffix)
100 try:
101 expected_image = _download_and_open_image(
102 expected_image_file, expected_image_url)
103 except Exception:
104 logging.exception('unable to download expected_image_url %s to file %s' %
105 (expected_image_url, expected_image_file))
106 raise
107 try:
108 actual_image = _download_and_open_image(
109 actual_image_file, actual_image_url)
110 except Exception:
111 logging.exception('unable to download actual_image_url %s to file %s' %
112 (actual_image_url, actual_image_file))
113 raise
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000114
epoger@google.com214a0242013-11-22 19:26:18 +0000115 # Generate the diff image (absolute diff at each pixel) and
116 # max_diff_per_channel.
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000117 diff_image = _generate_image_diff(actual_image, expected_image)
epoger@google.com214a0242013-11-22 19:26:18 +0000118 diff_histogram = diff_image.histogram()
119 (diff_width, diff_height) = diff_image.size
120 self._weighted_diff_measure = _calculate_weighted_diff_metric(
121 diff_histogram, diff_width * diff_height)
122 self._max_diff_per_channel = _max_per_band(diff_histogram)
123
124 # Generate the whitediff image (any differing pixels show as white).
125 # This is tricky, because when you convert color images to grayscale or
126 # black & white in PIL, it has its own ideas about thresholds.
127 # We have to force it: if a pixel has any color at all, it's a '1'.
128 bands = diff_image.split()
129 graydiff_image = ImageChops.lighter(ImageChops.lighter(
130 bands[0], bands[1]), bands[2])
131 whitediff_image = (graydiff_image.point(lambda p: p > 0 and VALUES_PER_BAND)
132 .convert('1', dither=Image.NONE))
133
commit-bot@chromium.org44546f82014-02-11 18:21:26 +0000134 # Calculate the perceptual difference percentage.
135 skpdiff_csv_dir = tempfile.mkdtemp()
136 try:
137 skpdiff_csv_output = os.path.join(skpdiff_csv_dir, 'skpdiff-output.csv')
commit-bot@chromium.org44546f82014-02-11 18:21:26 +0000138 expected_img = os.path.join(storage_root, expected_images_subdir,
139 str(expected_image_locator) + image_suffix)
140 actual_img = os.path.join(storage_root, actual_images_subdir,
141 str(actual_image_locator) + image_suffix)
142 find_run_binary.run_command(
commit-bot@chromium.org4d0f0082014-02-18 14:38:22 +0000143 [SKPDIFF_BINARY, '-p', expected_img, actual_img,
commit-bot@chromium.org44546f82014-02-11 18:21:26 +0000144 '--csv', skpdiff_csv_output, '-d', 'perceptual'])
145 with contextlib.closing(open(skpdiff_csv_output)) as csv_file:
146 for row in csv.DictReader(csv_file):
147 perceptual_similarity = float(row[' perceptual'].strip())
148 if not 0 <= perceptual_similarity <= 1:
149 # skpdiff outputs -1 if the images are different sizes. Treat any
150 # output that does not lie in [0, 1] as having 0% perceptual
151 # similarity.
152 perceptual_similarity = 0
153 # skpdiff returns the perceptual similarity, convert it to get the
154 # perceptual difference percentage.
155 self._perceptual_difference = 100 - (perceptual_similarity * 100)
156 finally:
157 shutil.rmtree(skpdiff_csv_dir)
158
epoger@google.com214a0242013-11-22 19:26:18 +0000159 # Final touches on diff_image: use whitediff_image as an alpha mask.
160 # Unchanged pixels are transparent; differing pixels are opaque.
161 diff_image.putalpha(whitediff_image)
162
163 # Store the diff and whitediff images generated above.
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000164 diff_image_locator = _get_difference_locator(
165 expected_image_locator=expected_image_locator,
166 actual_image_locator=actual_image_locator)
rmistry@google.com5861e522013-12-21 19:07:40 +0000167 basename = str(diff_image_locator) + image_suffix
epoger@google.com214a0242013-11-22 19:26:18 +0000168 _save_image(diff_image, os.path.join(
169 storage_root, DIFFS_SUBDIR, basename))
170 _save_image(whitediff_image, os.path.join(
171 storage_root, WHITEDIFFS_SUBDIR, basename))
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000172
173 # Calculate difference metrics.
174 (self._width, self._height) = diff_image.size
epoger@google.com214a0242013-11-22 19:26:18 +0000175 self._num_pixels_differing = (
176 whitediff_image.histogram()[VALUES_PER_BAND - 1])
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000177
178 def get_num_pixels_differing(self):
179 """Returns the absolute number of pixels that differ."""
180 return self._num_pixels_differing
181
182 def get_percent_pixels_differing(self):
183 """Returns the percentage of pixels that differ, as a float between
184 0 and 100 (inclusive)."""
185 return ((float(self._num_pixels_differing) * 100) /
186 (self._width * self._height))
187
commit-bot@chromium.org44546f82014-02-11 18:21:26 +0000188 def get_perceptual_difference(self):
189 """Returns the perceptual difference percentage."""
190 return self._perceptual_difference
191
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000192 def get_weighted_diff_measure(self):
193 """Returns a weighted measure of image diffs, as a float between 0 and 100
commit-bot@chromium.org4d0f0082014-02-18 14:38:22 +0000194 (inclusive).
195
196 TODO(epoger): Delete this function, now that we have perceptual diff?
197 """
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000198 return self._weighted_diff_measure
199
epoger@google.com214a0242013-11-22 19:26:18 +0000200 def get_max_diff_per_channel(self):
201 """Returns the maximum difference between the expected and actual images
202 for each R/G/B channel, as a list."""
203 return self._max_diff_per_channel
204
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +0000205 def as_dict(self):
206 """Returns a dictionary representation of this DiffRecord, as needed when
207 constructing the JSON representation."""
208 return {
commit-bot@chromium.org16f41802014-02-26 19:05:20 +0000209 KEY__DIFFERENCE_DATA__NUM_DIFF_PIXELS: self._num_pixels_differing,
210 KEY__DIFFERENCE_DATA__PERCENT_DIFF_PIXELS:
211 self.get_percent_pixels_differing(),
212 KEY__DIFFERENCE_DATA__WEIGHTED_DIFF: self.get_weighted_diff_measure(),
213 KEY__DIFFERENCE_DATA__MAX_DIFF_PER_CHANNEL: self._max_diff_per_channel,
214 KEY__DIFFERENCE_DATA__PERCEPTUAL_DIFF: self._perceptual_difference,
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +0000215 }
216
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000217
218class ImageDiffDB(object):
219 """ Calculates differences between image pairs, maintaining a database of
220 them for download."""
221
222 def __init__(self, storage_root):
223 """
224 Args:
225 storage_root: string; root path within the DB will store all of its stuff
226 """
227 self._storage_root = storage_root
228
229 # Dictionary of DiffRecords, keyed by (expected_image_locator,
230 # actual_image_locator) tuples.
231 self._diff_dict = {}
232
233 def add_image_pair(self,
234 expected_image_url, expected_image_locator,
235 actual_image_url, actual_image_locator):
236 """Download this pair of images (unless we already have them on local disk),
237 and prepare a DiffRecord for them.
238
239 TODO(epoger): Make this asynchronously download images, rather than blocking
240 until the images have been downloaded and processed.
241 When we do that, we should probably add a new method that will block
242 until all of the images have been downloaded and processed. Otherwise,
243 we won't know when it's safe to start calling get_diff_record().
244 jcgregorio notes: maybe just make ImageDiffDB thread-safe and create a
245 thread-pool/worker queue at a higher level that just uses ImageDiffDB?
246
247 Args:
248 expected_image_url: file or HTTP url from which we will download the
249 expected image
250 expected_image_locator: a unique ID string under which we will store the
251 expected image within storage_root (probably including a checksum to
252 guarantee uniqueness)
253 actual_image_url: file or HTTP url from which we will download the
254 actual image
255 actual_image_locator: a unique ID string under which we will store the
256 actual image within storage_root (probably including a checksum to
257 guarantee uniqueness)
258 """
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +0000259 expected_image_locator = _sanitize_locator(expected_image_locator)
260 actual_image_locator = _sanitize_locator(actual_image_locator)
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000261 key = (expected_image_locator, actual_image_locator)
262 if not key in self._diff_dict:
263 try:
264 new_diff_record = DiffRecord(
265 self._storage_root,
266 expected_image_url=expected_image_url,
267 expected_image_locator=expected_image_locator,
268 actual_image_url=actual_image_url,
269 actual_image_locator=actual_image_locator)
commit-bot@chromium.orga47e7ac2013-12-19 20:01:34 +0000270 except Exception:
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000271 logging.exception('got exception while creating new DiffRecord')
272 return
273 self._diff_dict[key] = new_diff_record
274
275 def get_diff_record(self, expected_image_locator, actual_image_locator):
276 """Returns the DiffRecord for this image pair.
277
278 Raises a KeyError if we don't have a DiffRecord for this image pair.
279 """
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +0000280 key = (_sanitize_locator(expected_image_locator),
281 _sanitize_locator(actual_image_locator))
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000282 return self._diff_dict[key]
283
284
285# Utility functions
286
epoger@google.com214a0242013-11-22 19:26:18 +0000287def _calculate_weighted_diff_metric(histogram, num_pixels):
288 """Given the histogram of a diff image (per-channel diff at each
289 pixel between two images), calculate the weighted diff metric (a
290 stab at how different the two images really are).
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000291
commit-bot@chromium.org4d0f0082014-02-18 14:38:22 +0000292 TODO(epoger): Delete this function, now that we have perceptual diff?
293
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000294 Args:
epoger@google.com214a0242013-11-22 19:26:18 +0000295 histogram: PIL histogram of a per-channel diff between two images
296 num_pixels: integer; the total number of pixels in the diff image
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000297
298 Returns: a weighted diff metric, as a float between 0 and 100 (inclusive).
299 """
epoger@google.com214a0242013-11-22 19:26:18 +0000300 # TODO(epoger): As a wild guess at an appropriate metric, weight each
301 # different pixel by the square of its delta value. (The more different
302 # a pixel is from its expectation, the more we care about it.)
epoger@google.com214a0242013-11-22 19:26:18 +0000303 assert(len(histogram) % VALUES_PER_BAND == 0)
304 num_bands = len(histogram) / VALUES_PER_BAND
305 max_diff = num_pixels * num_bands * (VALUES_PER_BAND - 1)**2
306 total_diff = 0
307 for index in xrange(len(histogram)):
308 total_diff += histogram[index] * (index % VALUES_PER_BAND)**2
309 return float(100 * total_diff) / max_diff
310
commit-bot@chromium.org16f41802014-02-26 19:05:20 +0000311
epoger@google.com214a0242013-11-22 19:26:18 +0000312def _max_per_band(histogram):
313 """Given the histogram of an image, return the maximum value of each band
314 (a.k.a. "color channel", such as R/G/B) across the entire image.
315
316 Args:
317 histogram: PIL histogram
318
319 Returns the maximum value of each band within the image histogram, as a list.
320 """
321 max_per_band = []
322 assert(len(histogram) % VALUES_PER_BAND == 0)
323 num_bands = len(histogram) / VALUES_PER_BAND
324 for band in xrange(num_bands):
325 # Assuming that VALUES_PER_BAND is 256...
326 # the 'R' band makes up indices 0-255 in the histogram,
327 # the 'G' band makes up indices 256-511 in the histogram,
328 # etc.
329 min_index = band * VALUES_PER_BAND
330 index = min_index + VALUES_PER_BAND
331 while index > min_index:
332 index -= 1
333 if histogram[index] > 0:
334 max_per_band.append(index - min_index)
335 break
336 return max_per_band
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000337
commit-bot@chromium.org16f41802014-02-26 19:05:20 +0000338
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000339def _generate_image_diff(image1, image2):
340 """Wrapper for ImageChops.difference(image1, image2) that will handle some
341 errors automatically, or at least yield more useful error messages.
342
343 TODO(epoger): Currently, some of the images generated by the bots are RGBA
344 and others are RGB. I'm not sure why that is. For now, to avoid confusion
345 within the UI, convert all to RGB when diffing.
346
347 Args:
348 image1: a PIL image object
349 image2: a PIL image object
350
351 Returns: per-pixel diffs between image1 and image2, as a PIL image object
352 """
353 try:
354 return ImageChops.difference(image1.convert('RGB'), image2.convert('RGB'))
355 except ValueError:
356 logging.error('Error diffing image1 [%s] and image2 [%s].' % (
357 repr(image1), repr(image2)))
358 raise
359
commit-bot@chromium.org16f41802014-02-26 19:05:20 +0000360
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000361def _download_and_open_image(local_filepath, url):
362 """Open the image at local_filepath; if there is no file at that path,
363 download it from url to that path and then open it.
364
365 Args:
366 local_filepath: path on local disk where the image should be stored
367 url: URL from which we can download the image if we don't have it yet
368
369 Returns: a PIL image object
370 """
371 if not os.path.exists(local_filepath):
372 _mkdir_unless_exists(os.path.dirname(local_filepath))
373 with contextlib.closing(urllib.urlopen(url)) as url_handle:
374 with open(local_filepath, 'wb') as file_handle:
375 shutil.copyfileobj(fsrc=url_handle, fdst=file_handle)
376 return _open_image(local_filepath)
377
commit-bot@chromium.org16f41802014-02-26 19:05:20 +0000378
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000379def _open_image(filepath):
380 """Wrapper for Image.open(filepath) that yields more useful error messages.
381
382 Args:
383 filepath: path on local disk to load image from
384
385 Returns: a PIL image object
386 """
387 try:
388 return Image.open(filepath)
389 except IOError:
commit-bot@chromium.orgda0ceb22014-03-26 13:38:29 +0000390 # If we are unable to load an image from the file, delete it from disk
391 # and we will try to fetch it again next time. Fixes http://skbug.com/2247
392 logging.error('IOError loading image file %s ; deleting it.' % filepath)
393 os.remove(filepath)
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000394 raise
395
commit-bot@chromium.org16f41802014-02-26 19:05:20 +0000396
epoger@google.com214a0242013-11-22 19:26:18 +0000397def _save_image(image, filepath, format='PNG'):
398 """Write an image to disk, creating any intermediate directories as needed.
399
400 Args:
401 image: a PIL image object
402 filepath: path on local disk to write image to
403 format: one of the PIL image formats, listed at
404 http://effbot.org/imagingbook/formats.htm
405 """
406 _mkdir_unless_exists(os.path.dirname(filepath))
407 image.save(filepath, format)
408
commit-bot@chromium.org16f41802014-02-26 19:05:20 +0000409
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000410def _mkdir_unless_exists(path):
411 """Unless path refers to an already-existing directory, create it.
412
413 Args:
414 path: path on local disk
415 """
416 if not os.path.isdir(path):
417 os.makedirs(path)
418
commit-bot@chromium.org16f41802014-02-26 19:05:20 +0000419
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +0000420def _sanitize_locator(locator):
421 """Returns a sanitized version of a locator (one in which we know none of the
422 characters will have special meaning in filenames).
423
424 Args:
425 locator: string, or something that can be represented as a string
426 """
427 return DISALLOWED_FILEPATH_CHAR_REGEX.sub('_', str(locator))
428
commit-bot@chromium.org16f41802014-02-26 19:05:20 +0000429
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000430def _get_difference_locator(expected_image_locator, actual_image_locator):
431 """Returns the locator string used to look up the diffs between expected_image
432 and actual_image.
433
commit-bot@chromium.org16f41802014-02-26 19:05:20 +0000434 We must keep this function in sync with getImageDiffRelativeUrl() in
435 static/loader.js
436
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000437 Args:
438 expected_image_locator: locator string pointing at expected image
439 actual_image_locator: locator string pointing at actual image
440
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +0000441 Returns: already-sanitized locator where the diffs between expected and
442 actual images can be found
epoger@google.com9dddf6f2013-11-08 16:25:25 +0000443 """
commit-bot@chromium.org9985ef52014-02-10 18:19:30 +0000444 return "%s-vs-%s" % (_sanitize_locator(expected_image_locator),
445 _sanitize_locator(actual_image_locator))