blob: 13e5069abc09fd79b2e6aa0a43464a390d15d196 [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é Fonsecaf87d8c62011-02-15 12:19:11 +000042
43
44thumb_size = 320, 320
45
46
José Fonseca0b956fd2011-06-04 22:51:45 +010047class Comparer:
48 '''Image comparer.'''
José Fonsecacbb46fa2011-05-21 18:47:21 +010049
José Fonseca0b956fd2011-06-04 22:51:45 +010050 def __init__(self, ref_image, src_image, alpha = False):
José Fonsecabcca5f72011-09-06 00:07:41 +010051 if isinstance(ref_image, basestring):
52 self.ref_im = Image.open(ref_image)
53 else:
54 self.ref_im = ref_image
55
56 if isinstance(src_image, basestring):
57 self.src_im = Image.open(src_image)
58 else:
59 self.src_im = src_image
José Fonsecacbb46fa2011-05-21 18:47:21 +010060
José Fonseca0b956fd2011-06-04 22:51:45 +010061 # Ignore
62 if not alpha:
63 self.ref_im = self.ref_im.convert('RGB')
64 self.src_im = self.src_im.convert('RGB')
José Fonsecacbb46fa2011-05-21 18:47:21 +010065
José Fonseca0b956fd2011-06-04 22:51:45 +010066 self.diff = ImageChops.difference(self.src_im, self.ref_im)
José Fonsecacbb46fa2011-05-21 18:47:21 +010067
José Fonseca0b956fd2011-06-04 22:51:45 +010068 def write_diff(self, diff_image, fuzz = 0.05):
69 # make a difference image similar to ImageMagick's compare utility
70 mask = ImageEnhance.Brightness(self.diff).enhance(1.0/fuzz)
71 mask = mask.convert('L')
José Fonsecacbb46fa2011-05-21 18:47:21 +010072
José Fonseca0b956fd2011-06-04 22:51:45 +010073 lowlight = Image.new('RGB', self.src_im.size, (0xff, 0xff, 0xff))
74 highlight = Image.new('RGB', self.src_im.size, (0xf1, 0x00, 0x1e))
75 diff_im = Image.composite(highlight, lowlight, mask)
José Fonsecacbb46fa2011-05-21 18:47:21 +010076
José Fonseca0b956fd2011-06-04 22:51:45 +010077 diff_im = Image.blend(self.src_im, diff_im, 0xcc/255.0)
78 diff_im.save(diff_image)
79
80 def precision(self):
81 # See also http://effbot.org/zone/pil-comparing-images.htm
82 h = self.diff.histogram()
83 square_error = 0
84 for i in range(1, 256):
85 square_error += sum(h[i : 3*256: 256])*i*i
86 rel_error = float(square_error*2 + 1) / float(self.diff.size[0]*self.diff.size[1]*3*255*255*2)
87 bits = -math.log(rel_error)/math.log(2.0)
88 return bits
89
90 def ae(self):
91 # Compute absolute error
92 # TODO: this is approximate due to the grayscale conversion
93 h = self.diff.convert('L').histogram()
94 ae = sum(h[int(255 * fuzz) + 1 : 256])
95 return ae
José Fonsecaf87d8c62011-02-15 12:19:11 +000096
97
98def surface(html, image):
José Fonseca0c881f72011-05-15 11:50:01 +010099 if True:
José Fonsecaf87d8c62011-02-15 12:19:11 +0000100 name, ext = os.path.splitext(image)
José Fonseca0c881f72011-05-15 11:50:01 +0100101 thumb = name + '.thumb' + ext
102 if os.path.exists(image) \
103 and (not os.path.exists(thumb) \
104 or os.path.getmtime(thumb) < os.path.getmtime(image)):
José Fonsecaf87d8c62011-02-15 12:19:11 +0000105 im = Image.open(image)
106 im.thumbnail(thumb_size)
107 im.save(thumb)
José Fonseca0c881f72011-05-15 11:50:01 +0100108 else:
109 thumb = image
110 html.write(' <td><a href="%s"><img src="%s"/></a></td>\n' % (image, thumb))
111
112
113def is_image(path):
114 return \
115 path.endswith('.png') \
116 and not path.endswith('.diff.png') \
117 and not path.endswith('.thumb.png')
118
119
120def find_images(prefix):
121 prefix = os.path.abspath(prefix)
122 if os.path.isdir(prefix):
123 prefix_dir = prefix
124 else:
125 prefix_dir = os.path.dirname(prefix)
126
127 images = []
128 for dirname, dirnames, filenames in os.walk(prefix_dir, followlinks=True):
129 for filename in filenames:
130 filepath = os.path.join(dirname, filename)
131 if filepath.startswith(prefix) and is_image(filepath):
132 images.append(filepath[len(prefix):])
133
134 return images
José Fonsecaf87d8c62011-02-15 12:19:11 +0000135
136
137def main():
José Fonsecacbb46fa2011-05-21 18:47:21 +0100138 global options
139
José Fonsecaf87d8c62011-02-15 12:19:11 +0000140 optparser = optparse.OptionParser(
Carl Worth905c1282011-11-14 11:05:11 -0800141 usage="\n\t%prog [options] <ref_prefix> <src_prefix>")
José Fonsecaf87d8c62011-02-15 12:19:11 +0000142 optparser.add_option(
143 '-o', '--output', metavar='FILE',
José Fonsecacbb46fa2011-05-21 18:47:21 +0100144 type="string", dest="output", default='index.html',
145 help="output filename [default: %default]")
José Fonsecaf87d8c62011-02-15 12:19:11 +0000146 optparser.add_option(
José Fonsecaf87d8c62011-02-15 12:19:11 +0000147 '-f', '--fuzz',
José Fonseca0b956fd2011-06-04 22:51:45 +0100148 type="float", dest="fuzz", default=0.05,
José Fonsecada3e8442011-06-03 19:50:34 +0100149 help="fuzz ratio [default: %default]")
José Fonsecacbb46fa2011-05-21 18:47:21 +0100150 optparser.add_option(
151 '--overwrite',
152 action="store_true", dest="overwrite", default=False,
153 help="overwrite")
José Fonsecaf87d8c62011-02-15 12:19:11 +0000154
155 (options, args) = optparser.parse_args(sys.argv[1:])
156
157 if len(args) != 2:
158 optparser.error('incorrect number of arguments')
159
José Fonseca2f6720e2011-04-13 15:57:15 +0100160 ref_prefix = args[0]
161 src_prefix = args[1]
José Fonsecaf87d8c62011-02-15 12:19:11 +0000162
José Fonseca0c881f72011-05-15 11:50:01 +0100163 ref_images = find_images(ref_prefix)
164 src_images = find_images(src_prefix)
165 images = list(set(ref_images).intersection(set(src_images)))
166 images.sort()
167
José Fonsecaf87d8c62011-02-15 12:19:11 +0000168 if options.output:
169 html = open(options.output, 'wt')
170 else:
171 html = sys.stdout
172 html.write('<html>\n')
173 html.write(' <body>\n')
174 html.write(' <table border="1">\n')
José Fonseca0c881f72011-05-15 11:50:01 +0100175 html.write(' <tr><th>%s</th><th>%s</th><th>&Delta;</th></tr>\n' % (ref_prefix, src_prefix))
176 for image in images:
177 ref_image = ref_prefix + image
178 src_image = src_prefix + image
179 root, ext = os.path.splitext(src_image)
180 delta_image = "%s.diff.png" % (root, )
José Fonsecaf87d8c62011-02-15 12:19:11 +0000181 if os.path.exists(ref_image) and os.path.exists(src_image):
José Fonsecacbb46fa2011-05-21 18:47:21 +0100182 if options.overwrite \
183 or not os.path.exists(delta_image) \
José Fonseca0c881f72011-05-15 11:50:01 +0100184 or (os.path.getmtime(delta_image) < os.path.getmtime(ref_image) \
185 and os.path.getmtime(delta_image) < os.path.getmtime(src_image)):
José Fonseca0b956fd2011-06-04 22:51:45 +0100186
187 comparer = Comparer(ref_image, src_image)
188 comparer.write_diff(delta_image, fuzz=options.fuzz)
José Fonseca0c881f72011-05-15 11:50:01 +0100189
José Fonsecaf87d8c62011-02-15 12:19:11 +0000190 html.write(' <tr>\n')
José Fonsecaf87d8c62011-02-15 12:19:11 +0000191 surface(html, ref_image)
192 surface(html, src_image)
193 surface(html, delta_image)
194 html.write(' </tr>\n')
195 html.flush()
José Fonsecaf87d8c62011-02-15 12:19:11 +0000196 html.write(' </table>\n')
197 html.write(' </body>\n')
198 html.write('</html>\n')
199
200
201if __name__ == '__main__':
202 main()