Home | History | Annotate | Download | only in operators
      1 # Copyright (c) 2011 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 """Compare two images for equality."""
      6 
      7 from PIL import Image
      8 from PIL import ImageChops
      9 
     10 
     11 def Compare(file1, file2, **kwargs):
     12   """Compares two images to see if they're identical.
     13 
     14   Args:
     15     file1: path to first image to compare
     16     file2: path to second image to compare
     17     kwargs: unused for this operator
     18 
     19   Returns:
     20     None if the images are identical
     21     A tuple of (errorstring, image) if they're not
     22   """
     23   kwargs = kwargs  # unused parameter
     24 
     25   im1 = Image.open(file1)
     26   im2 = Image.open(file2)
     27 
     28   if im1.size != im2.size:
     29     return ("The images are of different size (%s vs %s)" %
     30             (im1.size, im2.size), im1)
     31 
     32   diff = ImageChops.difference(im1, im2)
     33 
     34   if max(diff.getextrema()) != (0, 0):
     35     return ("The images differ", diff)
     36   else:
     37     return None
     38