Index: chrome/test/functional/ispy/image_tools.py |
diff --git a/chrome/test/functional/ispy/image_tools.py b/chrome/test/functional/ispy/image_tools.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8dbd6fe7380860de7061d69e7411ef8176b448ca |
--- /dev/null |
+++ b/chrome/test/functional/ispy/image_tools.py |
@@ -0,0 +1,206 @@ |
+# Copyright (c) 2013 The Chromium Authos. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+"""Utilities for performing pixel-by-pixel image comparision.""" |
+ |
+import itertools |
+import PIL |
+from PIL import Image |
+ |
+ |
+def _AreTheSameSize(images): |
+ """Returns whether a set of images are the size size. |
+ |
+ Args: |
+ images: a list of images to compare. |
+ |
+ Returns: |
+ boolean. |
+ |
+ Raises: |
+ Exception: One image or fewer is passed in. |
+ """ |
+ if len(images) > 1: |
+ return all(images[0].size == img.size for img in images[1:]) |
+ else: |
+ raise Exception('No images passed in.') |
+ |
+ |
+def _GetDifferenceWithMask(image1, image2, mask=None, |
+ masked_color=(0, 0, 0), |
+ same_color=(0, 0, 0), |
+ different_color=(255, 255, 255)): |
+ """Returns an image representing the difference between the two images. |
+ |
+ This function computes the difference between two images taking into |
+ account a mask if it is provided. The final three arguments represent |
+ the coloration of the generated image. |
+ |
+ Args: |
+ image1: the first image to compare. |
+ image2: the second image to compare. |
+ mask: an optional mask to take into consideration. |
+ masked_color: the color of a masked section in the resulting image. |
+ same_color: the color of an unmasked section that is the same. |
+ between images 1 and 2 in the resulting image. |
+ different_color: the color of an unmasked section that is different |
+ between images 1 and 2 in the resulting image. |
+ |
+ Returns: |
+ an image repesenting the difference between the two images. |
+ |
+ Raises: |
+ Exception: if image1, image2, and mask are not the same size. |
+ """ |
+ if mask: |
+ if not _AreTheSameSize([image1, image2, mask]): |
+ raise Exception('images and mask must be the same size.') |
+ image_diff = PIL.Image.new('RGB', image1.size, (0, 0, 0)) |
+ data = list( |
craigdh
2013/06/18 19:16:11
use [] instead of list(). That will result in this
cgrimm
2013/06/18 20:21:01
Done.
|
+ masked_color if m == (255, 255, 255) |
+ else same_color if px1 == px2 |
+ else different_color |
+ for m, px1, px2 in itertools.izip(mask.getdata(), |
+ image1.getdata(), |
+ image2.getdata()) |
+ ) |
+ image_diff.putdata(data) |
+ return image_diff |
+ else: |
+ if not _AreTheSameSize([image1, image2]): |
+ raise Exception('images must be the same size.') |
+ image_diff = PIL.Image.new('RGB', image1.size, (0, 0, 0)) |
+ data = list( |
craigdh
2013/06/18 19:16:11
ditto
cgrimm
2013/06/18 20:21:01
Done.
|
+ same_color if px1 == px2 |
+ else different_color |
+ for px1, px2 in itertools.izip(image1.getdata(), |
+ image2.getdata()) |
+ ) |
+ image_diff.putdata(data) |
+ return image_diff |
+ |
+ |
+def CreateMask(images): |
+ """Computes a mask for a set of images. |
+ |
+ Returns an image that is computed from the images |
+ passed in. The mask is generated by continually |
craigdh
2013/06/18 19:16:11
Just say the mask will have a white pixel anywhere
cgrimm
2013/06/18 20:21:01
Done.
|
+ finding the difference between two images in the list |
+ with respect to a mask which is initialized as a black |
+ image, then setting the value of the mask to the result. |
+ |
+ Args: |
+ images: the images to compute the mask from. |
+ |
+ Returns: |
+ an image that is a mask of the passed in images. |
+ |
+ Raises: |
+ Exception: if the images passed in are not of the same size. |
+ Exception: if fewer than two images are passed in. |
+ """ |
+ if len(images) < 2: |
+ raise Exception('mask must be created from two or more images.') |
+ mask = Image.new('RGB', images[0].size, (0, 0, 0)) |
+ image = images[0] |
+ for other_image in images[1:]: |
+ mask = _GetDifferenceWithMask( |
+ image, |
+ other_image, |
+ mask, |
+ masked_color=(255, 255, 255)) |
+ return mask |
+ |
+ |
+def VisualizeImageDifferences(image1, image2, mask=None): |
+ """Returns an image repesenting the unmasked differences between two images. |
+ |
+ Iterates through the pixel values of two images and an optional |
+ mask. If the pixel values are the same, or the pixel is masked, |
+ (0,0,0) is stored for that pixel. Otherwise, (255,255,255) is stored. |
+ This ultimately produces an image where unmasked differences between |
+ the two images are white pixels, and everything else is black. |
+ |
+ Args: |
+ image1: an Image |
+ image2: another Image of the same size as image1. |
+ mask: an optional mask that represents parts of the two |
+ images to ignore when generating the difference image. |
+ |
+ Returns: |
+ a black and white image representing the unmasked difference between |
+ the two input images. |
+ |
+ Raises: |
+ Exception: if the two images and optional mask are different sizes. |
+ """ |
+ return _GetDifferenceWithMask(image1, image2, mask) |
+ |
+ |
+def TotalDifferentPixels(image1, image2, mask=None): |
+ """Computes the number of different pixels between two images. |
+ |
+ Args: |
+ image1: the first RGB PIL.Image to be compared. |
+ image2: the second RGB PIL.Image to be compared. |
+ mask: an optional mask to occlude parts of the images from calculation. |
craigdh
2013/06/18 19:16:11
Mention the format of the mask. Is it an image or
cgrimm
2013/06/18 20:21:01
Done.
|
+ |
+ Returns: |
+ the number of differing pixels between the images. |
+ |
+ Raises: |
+ Exception: if the images to be compared and the mask are not the same size. |
+ """ |
+ if mask: |
craigdh
2013/06/18 19:16:11
Entirely different codepaths are being followed de
cgrimm
2013/06/18 20:21:01
Similar problem in _GetDifferenceWithMask, fixed b
|
+ if _AreTheSameSize([image1, image2, mask]): |
+ return sum([ |
craigdh
2013/06/18 19:16:11
remove the []. No need to create a list in memory,
cgrimm
2013/06/18 20:21:01
Done.
|
+ 0 if m == (255, 255, 255) |
+ else 1 if px1 != px2 |
+ else 0 |
+ for px1, px2, m in itertools.izip( |
+ image1.getdata(), |
+ image2.getdata(), |
+ mask.getdata() |
+ ) |
+ ]) |
+ else: |
+ raise Exception('images and mask must be the same size') |
+ else: |
+ if _AreTheSameSize([image1, image2]): |
+ return sum([ |
craigdh
2013/06/18 19:16:11
ditto
cgrimm
2013/06/18 20:21:01
Done.
|
+ 1 if px1 != px2 |
+ else 0 |
+ for px1, px2 in itertools.izip( |
+ image1.getdata(), |
+ image2.getdata() |
+ ) |
+ ]) |
+ else: |
+ raise Exception('images must be the same size') |
+ |
+ |
+def SameImage(image1, image2, max_different_pixels=0, mask=None): |
+ """Returns a boolean representing whether the images are the same. |
+ |
+ Returns a boolean indicating whether two images are similar |
+ enough to be considered the same. Essentially wraps the |
+ TotalDifferentPixels function and adds a max_different_pixels |
+ cutoff for whether or not the images are the same. |
+ |
+ Args: |
+ image1: an RGB PIL.Image to compare. |
+ image2: an RGB PIL.Image to compare. |
+ max_different_pixels: a number that is the cutoff for whether or not |
+ the two images are the same. |
+ mask: an optional Image that occludes parts of the images from |
+ the calculation. |
+ |
+ Returns: |
+ True if the images are similar, False otherwise. |
+ |
+ Raises: |
+ Exception: if the images (and mask) are different sizes. |
+ """ |
+ different_pixels = TotalDifferentPixels(image1, image2, mask) |
+ return different_pixels <= max_different_pixels |