blob: 97fbeb8d81501e28d5c7bc1ecc791034769e54d8 [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
39import Image
José Fonsecacbb46fa2011-05-21 18:47:21 +010040import ImageChops
41import ImageEnhance
José Fonsecaf87d8c62011-02-15 12:19:11 +000042
43
44thumb_size = 320, 320
45
46
José Fonsecacbb46fa2011-05-21 18:47:21 +010047def compare(ref_image, src_image, delta_image):
48 ref_im = Image.open(ref_image)
49 src_im = Image.open(src_image)
50
51 ref_im = ref_im.convert('RGB')
52 src_im = src_im.convert('RGB')
53
54 diff = ImageChops.difference(src_im, ref_im)
55
José Fonseca3a136e82011-06-03 19:51:56 +010056 # make a difference image similar to ImageMagick's compare utility
José Fonsecada3e8442011-06-03 19:50:34 +010057 mask = ImageEnhance.Brightness(diff).enhance(1.0/options.fuzz)
José Fonsecacbb46fa2011-05-21 18:47:21 +010058 mask = mask.convert('L')
59
60 lowlight = Image.new('RGB', src_im.size, (0xff, 0xff, 0xff))
61 highlight = Image.new('RGB', src_im.size, (0xf1, 0x00, 0x1e))
62 delta_im = Image.composite(highlight, lowlight, mask)
63
64 delta_im = Image.blend(src_im, delta_im, 0xcc/255.0)
65 delta_im.save(delta_image)
66
67 # See also http://effbot.org/zone/pil-comparing-images.htm
68 # TODO: this is approximate due to the grayscale conversion
69 h = diff.convert('L').histogram()
José Fonsecada3e8442011-06-03 19:50:34 +010070 ae = sum(h[int(255 * options.fuzz) + 1 : 256])
José Fonsecacbb46fa2011-05-21 18:47:21 +010071 return ae
José Fonsecaf87d8c62011-02-15 12:19:11 +000072
73
74def surface(html, image):
José Fonseca0c881f72011-05-15 11:50:01 +010075 if True:
José Fonsecaf87d8c62011-02-15 12:19:11 +000076 name, ext = os.path.splitext(image)
José Fonseca0c881f72011-05-15 11:50:01 +010077 thumb = name + '.thumb' + ext
78 if os.path.exists(image) \
79 and (not os.path.exists(thumb) \
80 or os.path.getmtime(thumb) < os.path.getmtime(image)):
José Fonsecaf87d8c62011-02-15 12:19:11 +000081 im = Image.open(image)
82 im.thumbnail(thumb_size)
83 im.save(thumb)
José Fonseca0c881f72011-05-15 11:50:01 +010084 else:
85 thumb = image
86 html.write(' <td><a href="%s"><img src="%s"/></a></td>\n' % (image, thumb))
87
88
89def is_image(path):
90 return \
91 path.endswith('.png') \
92 and not path.endswith('.diff.png') \
93 and not path.endswith('.thumb.png')
94
95
96def find_images(prefix):
97 prefix = os.path.abspath(prefix)
98 if os.path.isdir(prefix):
99 prefix_dir = prefix
100 else:
101 prefix_dir = os.path.dirname(prefix)
102
103 images = []
104 for dirname, dirnames, filenames in os.walk(prefix_dir, followlinks=True):
105 for filename in filenames:
106 filepath = os.path.join(dirname, filename)
107 if filepath.startswith(prefix) and is_image(filepath):
108 images.append(filepath[len(prefix):])
109
110 return images
José Fonsecaf87d8c62011-02-15 12:19:11 +0000111
112
113def main():
José Fonsecacbb46fa2011-05-21 18:47:21 +0100114 global options
115
José Fonsecaf87d8c62011-02-15 12:19:11 +0000116 optparser = optparse.OptionParser(
José Fonseca0c881f72011-05-15 11:50:01 +0100117 usage="\n\t%prog [options] <ref_prefix> <src_prefix>",
José Fonsecaf87d8c62011-02-15 12:19:11 +0000118 version="%%prog")
119 optparser.add_option(
120 '-o', '--output', metavar='FILE',
José Fonsecacbb46fa2011-05-21 18:47:21 +0100121 type="string", dest="output", default='index.html',
122 help="output filename [default: %default]")
José Fonsecaf87d8c62011-02-15 12:19:11 +0000123 optparser.add_option(
José Fonsecaf87d8c62011-02-15 12:19:11 +0000124 '-f', '--fuzz',
José Fonsecada3e8442011-06-03 19:50:34 +0100125 type="float", dest="fuzz", default=.05,
126 help="fuzz ratio [default: %default]")
José Fonsecacbb46fa2011-05-21 18:47:21 +0100127 optparser.add_option(
128 '--overwrite',
129 action="store_true", dest="overwrite", default=False,
130 help="overwrite")
José Fonsecaf87d8c62011-02-15 12:19:11 +0000131
132 (options, args) = optparser.parse_args(sys.argv[1:])
133
134 if len(args) != 2:
135 optparser.error('incorrect number of arguments')
136
José Fonseca2f6720e2011-04-13 15:57:15 +0100137 ref_prefix = args[0]
138 src_prefix = args[1]
José Fonsecaf87d8c62011-02-15 12:19:11 +0000139
José Fonseca0c881f72011-05-15 11:50:01 +0100140 ref_images = find_images(ref_prefix)
141 src_images = find_images(src_prefix)
142 images = list(set(ref_images).intersection(set(src_images)))
143 images.sort()
144
José Fonsecaf87d8c62011-02-15 12:19:11 +0000145 if options.output:
146 html = open(options.output, 'wt')
147 else:
148 html = sys.stdout
149 html.write('<html>\n')
150 html.write(' <body>\n')
151 html.write(' <table border="1">\n')
José Fonseca0c881f72011-05-15 11:50:01 +0100152 html.write(' <tr><th>%s</th><th>%s</th><th>&Delta;</th></tr>\n' % (ref_prefix, src_prefix))
153 for image in images:
154 ref_image = ref_prefix + image
155 src_image = src_prefix + image
156 root, ext = os.path.splitext(src_image)
157 delta_image = "%s.diff.png" % (root, )
José Fonsecaf87d8c62011-02-15 12:19:11 +0000158 if os.path.exists(ref_image) and os.path.exists(src_image):
José Fonsecacbb46fa2011-05-21 18:47:21 +0100159 if options.overwrite \
160 or not os.path.exists(delta_image) \
José Fonseca0c881f72011-05-15 11:50:01 +0100161 or (os.path.getmtime(delta_image) < os.path.getmtime(ref_image) \
162 and os.path.getmtime(delta_image) < os.path.getmtime(src_image)):
José Fonsecacbb46fa2011-05-21 18:47:21 +0100163 compare(ref_image, src_image, delta_image)
José Fonseca0c881f72011-05-15 11:50:01 +0100164
José Fonsecaf87d8c62011-02-15 12:19:11 +0000165 html.write(' <tr>\n')
José Fonsecaf87d8c62011-02-15 12:19:11 +0000166 surface(html, ref_image)
167 surface(html, src_image)
168 surface(html, delta_image)
169 html.write(' </tr>\n')
170 html.flush()
José Fonsecaf87d8c62011-02-15 12:19:11 +0000171 html.write(' </table>\n')
172 html.write(' </body>\n')
173 html.write('</html>\n')
174
175
176if __name__ == '__main__':
177 main()