blob: 53d9f826579f70fcdbbdccf9f33f242e96a024f1 [file] [log] [blame]
José Fonsecaf87d8c62011-02-15 12:19:11 +00001#!/usr/bin/env python
2##########################################################################
3#
José Fonsecacbb46fa2011-05-21 18:47:21 +01004# Copyright 2011 Jose Fonseca
José Fonsecaf87d8c62011-02-15 12:19:11 +00005# Copyright 2008-2009 VMware, Inc.
6# All Rights Reserved.
7#
8# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and associated documentation files (the "Software"), to deal
10# in the Software without restriction, including without limitation the rights
11# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12# copies of the Software, and to permit persons to whom the Software is
13# furnished to do so, subject to the following conditions:
14#
15# The above copyright notice and this permission notice shall be included in
16# all copies or substantial portions of the Software.
17#
18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24# THE SOFTWARE.
25#
26##########################################################################/
27
28
José Fonsecabe591db2011-05-22 00:37:12 +010029'''Snapshot (image) comparison script.
30'''
31
32
José Fonsecaf87d8c62011-02-15 12:19:11 +000033import sys
34import os.path
José Fonsecaf87d8c62011-02-15 12:19:11 +000035import optparse
José Fonsecacbb46fa2011-05-21 18:47:21 +010036import math
37import operator
José Fonsecaf87d8c62011-02-15 12:19:11 +000038
José Fonseca6d5954b2011-07-13 00:08:21 +010039from PIL import Image
40from PIL import ImageChops
41from PIL import ImageEnhance
José Fonsecae9fcdcf2011-12-14 23:18:49 +000042from PIL import ImageFilter
José Fonsecaf87d8c62011-02-15 12:19:11 +000043
44
45thumb_size = 320, 320
46
José Fonsecae9fcdcf2011-12-14 23:18:49 +000047gaussian_kernel = ImageFilter.Kernel((3, 3), [1, 2, 1, 2, 4, 2, 1, 2, 1], 16)
José Fonsecaf87d8c62011-02-15 12:19:11 +000048
José Fonseca0b956fd2011-06-04 22:51:45 +010049class Comparer:
50 '''Image comparer.'''
José Fonsecacbb46fa2011-05-21 18:47:21 +010051
José Fonseca0b956fd2011-06-04 22:51:45 +010052 def __init__(self, ref_image, src_image, alpha = False):
José Fonsecabcca5f72011-09-06 00:07:41 +010053 if isinstance(ref_image, basestring):
54 self.ref_im = Image.open(ref_image)
55 else:
56 self.ref_im = ref_image
57
58 if isinstance(src_image, basestring):
59 self.src_im = Image.open(src_image)
60 else:
61 self.src_im = src_image
José Fonsecacbb46fa2011-05-21 18:47:21 +010062
José Fonseca0b956fd2011-06-04 22:51:45 +010063 # Ignore
64 if not alpha:
65 self.ref_im = self.ref_im.convert('RGB')
66 self.src_im = self.src_im.convert('RGB')
José Fonsecacbb46fa2011-05-21 18:47:21 +010067
José Fonseca0b956fd2011-06-04 22:51:45 +010068 self.diff = ImageChops.difference(self.src_im, self.ref_im)
José Fonsecacbb46fa2011-05-21 18:47:21 +010069
José Fonsecaff1c0f22011-12-11 12:34:40 +000070 def size_mismatch(self):
71 return self.ref_im.size != self.src_im.size
72
José Fonseca0b956fd2011-06-04 22:51:45 +010073 def write_diff(self, diff_image, fuzz = 0.05):
74 # make a difference image similar to ImageMagick's compare utility
75 mask = ImageEnhance.Brightness(self.diff).enhance(1.0/fuzz)
76 mask = mask.convert('L')
José Fonsecacbb46fa2011-05-21 18:47:21 +010077
José Fonseca0b956fd2011-06-04 22:51:45 +010078 lowlight = Image.new('RGB', self.src_im.size, (0xff, 0xff, 0xff))
79 highlight = Image.new('RGB', self.src_im.size, (0xf1, 0x00, 0x1e))
80 diff_im = Image.composite(highlight, lowlight, mask)
José Fonsecacbb46fa2011-05-21 18:47:21 +010081
José Fonseca0b956fd2011-06-04 22:51:45 +010082 diff_im = Image.blend(self.src_im, diff_im, 0xcc/255.0)
83 diff_im.save(diff_image)
84
José Fonsecae9fcdcf2011-12-14 23:18:49 +000085 def precision(self, filter=False):
José Fonsecaff1c0f22011-12-11 12:34:40 +000086 if self.size_mismatch():
87 return 0.0
88
José Fonsecae9fcdcf2011-12-14 23:18:49 +000089 diff = self.diff
90 if filter:
91 diff = diff.filter(gaussian_kernel)
92
José Fonseca0b956fd2011-06-04 22:51:45 +010093 # See also http://effbot.org/zone/pil-comparing-images.htm
José Fonsecae9fcdcf2011-12-14 23:18:49 +000094 h = diff.histogram()
José Fonseca0b956fd2011-06-04 22:51:45 +010095 square_error = 0
96 for i in range(1, 256):
97 square_error += sum(h[i : 3*256: 256])*i*i
98 rel_error = float(square_error*2 + 1) / float(self.diff.size[0]*self.diff.size[1]*3*255*255*2)
99 bits = -math.log(rel_error)/math.log(2.0)
100 return bits
101
José Fonseca01b8c7b2011-12-04 15:31:31 +0000102 def ae(self, fuzz = 0.05):
José Fonseca0b956fd2011-06-04 22:51:45 +0100103 # Compute absolute error
José Fonsecaff1c0f22011-12-11 12:34:40 +0000104
105 if self.size_mismatch():
106 return sys.maxint
107
José Fonseca0b956fd2011-06-04 22:51:45 +0100108 # TODO: this is approximate due to the grayscale conversion
109 h = self.diff.convert('L').histogram()
110 ae = sum(h[int(255 * fuzz) + 1 : 256])
111 return ae
José Fonsecaf87d8c62011-02-15 12:19:11 +0000112
113
114def surface(html, image):
José Fonseca0c881f72011-05-15 11:50:01 +0100115 if True:
José Fonsecaf87d8c62011-02-15 12:19:11 +0000116 name, ext = os.path.splitext(image)
José Fonseca0c881f72011-05-15 11:50:01 +0100117 thumb = name + '.thumb' + ext
118 if os.path.exists(image) \
119 and (not os.path.exists(thumb) \
120 or os.path.getmtime(thumb) < os.path.getmtime(image)):
José Fonsecaf87d8c62011-02-15 12:19:11 +0000121 im = Image.open(image)
122 im.thumbnail(thumb_size)
123 im.save(thumb)
José Fonseca0c881f72011-05-15 11:50:01 +0100124 else:
125 thumb = image
126 html.write(' <td><a href="%s"><img src="%s"/></a></td>\n' % (image, thumb))
127
128
129def is_image(path):
José Fonseca63f08472011-12-13 15:53:49 +0000130 name = os.path.basename(path)
131 name, ext1 = os.path.splitext(name)
132 name, ext2 = os.path.splitext(name)
José Fonseca63f08472011-12-13 15:53:49 +0000133 return ext1 in ('.png', '.bmp') and ext2 not in ('.diff', '.thumb')
José Fonseca0c881f72011-05-15 11:50:01 +0100134
135
136def find_images(prefix):
137 prefix = os.path.abspath(prefix)
138 if os.path.isdir(prefix):
139 prefix_dir = prefix
140 else:
141 prefix_dir = os.path.dirname(prefix)
142
143 images = []
144 for dirname, dirnames, filenames in os.walk(prefix_dir, followlinks=True):
145 for filename in filenames:
146 filepath = os.path.join(dirname, filename)
147 if filepath.startswith(prefix) and is_image(filepath):
148 images.append(filepath[len(prefix):])
149
150 return images
José Fonsecaf87d8c62011-02-15 12:19:11 +0000151
152
153def main():
José Fonsecacbb46fa2011-05-21 18:47:21 +0100154 global options
155
José Fonsecaf87d8c62011-02-15 12:19:11 +0000156 optparser = optparse.OptionParser(
Carl Worth905c1282011-11-14 11:05:11 -0800157 usage="\n\t%prog [options] <ref_prefix> <src_prefix>")
José Fonsecaf87d8c62011-02-15 12:19:11 +0000158 optparser.add_option(
159 '-o', '--output', metavar='FILE',
José Fonsecacbb46fa2011-05-21 18:47:21 +0100160 type="string", dest="output", default='index.html',
161 help="output filename [default: %default]")
José Fonsecaf87d8c62011-02-15 12:19:11 +0000162 optparser.add_option(
José Fonsecaf87d8c62011-02-15 12:19:11 +0000163 '-f', '--fuzz',
José Fonseca0b956fd2011-06-04 22:51:45 +0100164 type="float", dest="fuzz", default=0.05,
José Fonsecada3e8442011-06-03 19:50:34 +0100165 help="fuzz ratio [default: %default]")
José Fonsecacbb46fa2011-05-21 18:47:21 +0100166 optparser.add_option(
José Fonseca07984732011-12-13 15:53:13 +0000167 '-a', '--alpha',
168 action="store_true", dest="alpha", default=False,
169 help="take alpha channel in consideration")
170 optparser.add_option(
José Fonsecacbb46fa2011-05-21 18:47:21 +0100171 '--overwrite',
172 action="store_true", dest="overwrite", default=False,
José Fonseca07984732011-12-13 15:53:13 +0000173 help="overwrite images")
José Fonsecaf87d8c62011-02-15 12:19:11 +0000174
175 (options, args) = optparser.parse_args(sys.argv[1:])
176
177 if len(args) != 2:
178 optparser.error('incorrect number of arguments')
179
José Fonseca2f6720e2011-04-13 15:57:15 +0100180 ref_prefix = args[0]
181 src_prefix = args[1]
José Fonsecaf87d8c62011-02-15 12:19:11 +0000182
José Fonseca0c881f72011-05-15 11:50:01 +0100183 ref_images = find_images(ref_prefix)
184 src_images = find_images(src_prefix)
185 images = list(set(ref_images).intersection(set(src_images)))
186 images.sort()
187
José Fonsecaf87d8c62011-02-15 12:19:11 +0000188 if options.output:
189 html = open(options.output, 'wt')
190 else:
191 html = sys.stdout
192 html.write('<html>\n')
193 html.write(' <body>\n')
194 html.write(' <table border="1">\n')
José Fonseca0c881f72011-05-15 11:50:01 +0100195 html.write(' <tr><th>%s</th><th>%s</th><th>&Delta;</th></tr>\n' % (ref_prefix, src_prefix))
196 for image in images:
197 ref_image = ref_prefix + image
198 src_image = src_prefix + image
199 root, ext = os.path.splitext(src_image)
200 delta_image = "%s.diff.png" % (root, )
José Fonsecaf87d8c62011-02-15 12:19:11 +0000201 if os.path.exists(ref_image) and os.path.exists(src_image):
José Fonsecacbb46fa2011-05-21 18:47:21 +0100202 if options.overwrite \
203 or not os.path.exists(delta_image) \
José Fonseca0c881f72011-05-15 11:50:01 +0100204 or (os.path.getmtime(delta_image) < os.path.getmtime(ref_image) \
205 and os.path.getmtime(delta_image) < os.path.getmtime(src_image)):
José Fonseca0b956fd2011-06-04 22:51:45 +0100206
José Fonseca07984732011-12-13 15:53:13 +0000207 comparer = Comparer(ref_image, src_image, options.alpha)
José Fonseca0b956fd2011-06-04 22:51:45 +0100208 comparer.write_diff(delta_image, fuzz=options.fuzz)
José Fonseca0c881f72011-05-15 11:50:01 +0100209
José Fonsecaf87d8c62011-02-15 12:19:11 +0000210 html.write(' <tr>\n')
José Fonsecaf87d8c62011-02-15 12:19:11 +0000211 surface(html, ref_image)
212 surface(html, src_image)
213 surface(html, delta_image)
214 html.write(' </tr>\n')
215 html.flush()
José Fonsecaf87d8c62011-02-15 12:19:11 +0000216 html.write(' </table>\n')
217 html.write(' </body>\n')
218 html.write('</html>\n')
219
220
221if __name__ == '__main__':
222 main()