blob: 420744344ed87acef72f270dc2ade30cf16a9e12 [file] [log] [blame]
Jose Fonseca247e1fa2019-04-28 14:14:44 +01001#!/usr/bin/env python3
José Fonsecaf87d8c62011-02-15 12:19:11 +00002##########################################################################
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
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010043from functools import reduce
José Fonsecaf87d8c62011-02-15 12:19:11 +000044
45
José Fonsecac9573b52013-04-18 20:30:49 +010046thumbSize = 320
José Fonsecaf87d8c62011-02-15 12:19:11 +000047
José Fonsecae9fcdcf2011-12-14 23:18:49 +000048gaussian_kernel = ImageFilter.Kernel((3, 3), [1, 2, 1, 2, 4, 2, 1, 2, 1], 16)
José Fonsecaf87d8c62011-02-15 12:19:11 +000049
José Fonseca0b956fd2011-06-04 22:51:45 +010050class Comparer:
51 '''Image comparer.'''
José Fonsecacbb46fa2011-05-21 18:47:21 +010052
José Fonseca0b956fd2011-06-04 22:51:45 +010053 def __init__(self, ref_image, src_image, alpha = False):
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010054 if isinstance(ref_image, str):
José Fonsecabcca5f72011-09-06 00:07:41 +010055 self.ref_im = Image.open(ref_image)
56 else:
57 self.ref_im = ref_image
58
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +010059 if isinstance(src_image, str):
José Fonsecabcca5f72011-09-06 00:07:41 +010060 self.src_im = Image.open(src_image)
61 else:
62 self.src_im = src_image
José Fonsecacbb46fa2011-05-21 18:47:21 +010063
José Fonseca0b956fd2011-06-04 22:51:45 +010064 # Ignore
65 if not alpha:
66 self.ref_im = self.ref_im.convert('RGB')
67 self.src_im = self.src_im.convert('RGB')
José Fonsecacbb46fa2011-05-21 18:47:21 +010068
José Fonseca0b956fd2011-06-04 22:51:45 +010069 self.diff = ImageChops.difference(self.src_im, self.ref_im)
José Fonsecacbb46fa2011-05-21 18:47:21 +010070
José Fonsecaff1c0f22011-12-11 12:34:40 +000071 def size_mismatch(self):
72 return self.ref_im.size != self.src_im.size
73
José Fonseca0b956fd2011-06-04 22:51:45 +010074 def write_diff(self, diff_image, fuzz = 0.05):
José Fonsecadbef9b62013-05-22 13:21:45 +010075 if self.size_mismatch():
76 return
77
Jose Fonseca9b7da9c2016-02-17 10:25:53 +000078 # Make a difference image similar to ImageMagick's compare utility.
79 #
80 # Basically produces a brightened/faded version of the source image,
81 # but where every pixel for which absolute error is larger than
82 # 255*fuzz will be colored strong red.
83
84 # Take the maximum error across all channels
85 diff_max = reduce(ImageChops.lighter, self.diff.split())
86
87 # Scale values so that pixels equal or above 255*fuzz become 255
88 mask = diff_max.point(lambda x: min(x/fuzz, 255), 'L')
José Fonsecacbb46fa2011-05-21 18:47:21 +010089
José Fonseca0b956fd2011-06-04 22:51:45 +010090 lowlight = Image.new('RGB', self.src_im.size, (0xff, 0xff, 0xff))
91 highlight = Image.new('RGB', self.src_im.size, (0xf1, 0x00, 0x1e))
92 diff_im = Image.composite(highlight, lowlight, mask)
José Fonsecacbb46fa2011-05-21 18:47:21 +010093
José Fonseca0b956fd2011-06-04 22:51:45 +010094 diff_im = Image.blend(self.src_im, diff_im, 0xcc/255.0)
95 diff_im.save(diff_image)
96
José Fonsecae9fcdcf2011-12-14 23:18:49 +000097 def precision(self, filter=False):
José Fonsecaff1c0f22011-12-11 12:34:40 +000098 if self.size_mismatch():
99 return 0.0
100
José Fonsecae9fcdcf2011-12-14 23:18:49 +0000101 diff = self.diff
102 if filter:
103 diff = diff.filter(gaussian_kernel)
104
José Fonseca0b956fd2011-06-04 22:51:45 +0100105 # See also http://effbot.org/zone/pil-comparing-images.htm
José Fonsecae9fcdcf2011-12-14 23:18:49 +0000106 h = diff.histogram()
José Fonseca0b956fd2011-06-04 22:51:45 +0100107 square_error = 0
108 for i in range(1, 256):
109 square_error += sum(h[i : 3*256: 256])*i*i
110 rel_error = float(square_error*2 + 1) / float(self.diff.size[0]*self.diff.size[1]*3*255*255*2)
111 bits = -math.log(rel_error)/math.log(2.0)
112 return bits
113
José Fonseca01b8c7b2011-12-04 15:31:31 +0000114 def ae(self, fuzz = 0.05):
José Fonseca0b956fd2011-06-04 22:51:45 +0100115 # Compute absolute error
José Fonsecaff1c0f22011-12-11 12:34:40 +0000116
117 if self.size_mismatch():
Piotr Podsiadły0b8b0192019-01-03 20:39:55 +0100118 return sys.maxsize
José Fonsecaff1c0f22011-12-11 12:34:40 +0000119
José Fonseca0b956fd2011-06-04 22:51:45 +0100120 # TODO: this is approximate due to the grayscale conversion
121 h = self.diff.convert('L').histogram()
122 ae = sum(h[int(255 * fuzz) + 1 : 256])
123 return ae
José Fonsecaf87d8c62011-02-15 12:19:11 +0000124
125
126def surface(html, image):
José Fonseca0c881f72011-05-15 11:50:01 +0100127 if True:
José Fonsecaf87d8c62011-02-15 12:19:11 +0000128 name, ext = os.path.splitext(image)
José Fonseca0c881f72011-05-15 11:50:01 +0100129 thumb = name + '.thumb' + ext
130 if os.path.exists(image) \
131 and (not os.path.exists(thumb) \
132 or os.path.getmtime(thumb) < os.path.getmtime(image)):
José Fonsecaf87d8c62011-02-15 12:19:11 +0000133 im = Image.open(image)
José Fonsecac9573b52013-04-18 20:30:49 +0100134 imageWidth, imageHeight = im.size
135 if imageWidth <= thumbSize and imageHeight <= thumbSize:
136 if imageWidth >= imageHeight:
137 imageHeight = imageHeight*thumbSize/imageWidth
138 imageWidth = thumbSize
139 else:
140 imageWidth = imageWidth*thumbSize/imageHeight
141 imageHeight = thumbSize
142 html.write(' <td><img src="%s" width="%u" height="%u"/></td>\n' % (image, imageWidth, imageHeight))
143 return
144
145 im.thumbnail((thumbSize, thumbSize))
Jose Fonseca1cffb702017-05-23 17:29:59 +0100146 try:
147 im.save(thumb)
148 except IOError:
149 thumb = image
José Fonseca0c881f72011-05-15 11:50:01 +0100150 else:
151 thumb = image
152 html.write(' <td><a href="%s"><img src="%s"/></a></td>\n' % (image, thumb))
153
154
155def is_image(path):
José Fonseca63f08472011-12-13 15:53:49 +0000156 name = os.path.basename(path)
157 name, ext1 = os.path.splitext(name)
158 name, ext2 = os.path.splitext(name)
José Fonseca63f08472011-12-13 15:53:49 +0000159 return ext1 in ('.png', '.bmp') and ext2 not in ('.diff', '.thumb')
José Fonseca0c881f72011-05-15 11:50:01 +0100160
161
162def find_images(prefix):
José Fonseca0c881f72011-05-15 11:50:01 +0100163 if os.path.isdir(prefix):
164 prefix_dir = prefix
165 else:
166 prefix_dir = os.path.dirname(prefix)
167
168 images = []
169 for dirname, dirnames, filenames in os.walk(prefix_dir, followlinks=True):
170 for filename in filenames:
171 filepath = os.path.join(dirname, filename)
172 if filepath.startswith(prefix) and is_image(filepath):
173 images.append(filepath[len(prefix):])
174
175 return images
José Fonsecaf87d8c62011-02-15 12:19:11 +0000176
177
178def main():
José Fonsecacbb46fa2011-05-21 18:47:21 +0100179 global options
180
José Fonsecaf87d8c62011-02-15 12:19:11 +0000181 optparser = optparse.OptionParser(
Carl Worth905c1282011-11-14 11:05:11 -0800182 usage="\n\t%prog [options] <ref_prefix> <src_prefix>")
José Fonsecaf87d8c62011-02-15 12:19:11 +0000183 optparser.add_option(
Carl Worthcbabe342012-04-09 13:45:33 -0700184 '-v', '--verbose',
185 action="store_true", dest="verbose", default=False,
186 help="verbose output")
187 optparser.add_option(
José Fonsecaf87d8c62011-02-15 12:19:11 +0000188 '-o', '--output', metavar='FILE',
José Fonsecacbb46fa2011-05-21 18:47:21 +0100189 type="string", dest="output", default='index.html',
190 help="output filename [default: %default]")
José Fonsecaf87d8c62011-02-15 12:19:11 +0000191 optparser.add_option(
José Fonsecaf87d8c62011-02-15 12:19:11 +0000192 '-f', '--fuzz',
José Fonseca0b956fd2011-06-04 22:51:45 +0100193 type="float", dest="fuzz", default=0.05,
José Fonsecada3e8442011-06-03 19:50:34 +0100194 help="fuzz ratio [default: %default]")
José Fonsecacbb46fa2011-05-21 18:47:21 +0100195 optparser.add_option(
José Fonseca07984732011-12-13 15:53:13 +0000196 '-a', '--alpha',
197 action="store_true", dest="alpha", default=False,
198 help="take alpha channel in consideration")
199 optparser.add_option(
José Fonsecacbb46fa2011-05-21 18:47:21 +0100200 '--overwrite',
201 action="store_true", dest="overwrite", default=False,
José Fonseca07984732011-12-13 15:53:13 +0000202 help="overwrite images")
Carl Worth78329e22012-04-09 11:24:52 -0700203 optparser.add_option(
204 '--show-all',
205 action="store_true", dest="show_all", default=False,
206 help="show all images, including similar ones")
José Fonsecaf87d8c62011-02-15 12:19:11 +0000207
208 (options, args) = optparser.parse_args(sys.argv[1:])
209
210 if len(args) != 2:
211 optparser.error('incorrect number of arguments')
212
José Fonseca2f6720e2011-04-13 15:57:15 +0100213 ref_prefix = args[0]
214 src_prefix = args[1]
José Fonsecaf87d8c62011-02-15 12:19:11 +0000215
José Fonseca0c881f72011-05-15 11:50:01 +0100216 ref_images = find_images(ref_prefix)
217 src_images = find_images(src_prefix)
Jose Fonseca898676e2016-03-14 10:51:54 +0000218 images = list(set(ref_images).union(set(src_images)))
José Fonseca0c881f72011-05-15 11:50:01 +0100219 images.sort()
220
José Fonsecaf87d8c62011-02-15 12:19:11 +0000221 if options.output:
222 html = open(options.output, 'wt')
223 else:
224 html = sys.stdout
225 html.write('<html>\n')
226 html.write(' <body>\n')
227 html.write(' <table border="1">\n')
Carl Worthae3981a2012-04-03 15:18:00 -0700228 html.write(' <tr><th>File</th><th>%s</th><th>%s</th><th>&Delta;</th></tr>\n' % (ref_prefix, src_prefix))
Carl Worthb3b6d732012-04-03 16:02:25 -0700229 failures = 0
José Fonseca0c881f72011-05-15 11:50:01 +0100230 for image in images:
231 ref_image = ref_prefix + image
232 src_image = src_prefix + image
233 root, ext = os.path.splitext(src_image)
234 delta_image = "%s.diff.png" % (root, )
José Fonsecaf87d8c62011-02-15 12:19:11 +0000235 if os.path.exists(ref_image) and os.path.exists(src_image):
Carl Worthcbabe342012-04-09 13:45:33 -0700236 if options.verbose:
237 sys.stdout.write('Comparing %s and %s ...' % (ref_image, src_image))
Carl Worthb3b6d732012-04-03 16:02:25 -0700238 comparer = Comparer(ref_image, src_image, options.alpha)
Carl Worth78329e22012-04-09 11:24:52 -0700239 match = comparer.ae(fuzz=options.fuzz) == 0
240 if match:
Carl Worthcbabe342012-04-09 13:45:33 -0700241 result = 'MATCH'
Carl Worthb3b6d732012-04-03 16:02:25 -0700242 bgcolor = '#20ff20'
243 else:
Carl Worthcbabe342012-04-09 13:45:33 -0700244 result = 'MISMATCH'
Carl Worthb3b6d732012-04-03 16:02:25 -0700245 failures += 1
246 bgcolor = '#ff2020'
Jose Fonseca898676e2016-03-14 10:51:54 +0000247 else:
Jose Fonseca7fed7ad2016-12-02 15:14:26 +0000248 comparer = None
249 match = None
Jose Fonseca898676e2016-03-14 10:51:54 +0000250 result = 'MISSING'
251 failures += 1
252 bgcolor = '#ff2020'
253
254 if options.verbose:
255 sys.stdout.write(' %s\n' % (result,))
256 html.write(' <tr>\n')
257 html.write(' <td bgcolor="%s"><a href="%s">%s<a/></td>\n' % (bgcolor, ref_image, image))
258 if not match or options.show_all:
Jose Fonseca7fed7ad2016-12-02 15:14:26 +0000259 if comparer is not None \
260 and (options.overwrite \
261 or not os.path.exists(delta_image) \
262 or (os.path.getmtime(delta_image) < os.path.getmtime(ref_image) \
263 and os.path.getmtime(delta_image) < os.path.getmtime(src_image))):
264 comparer.write_diff(delta_image, fuzz=options.fuzz)
Jose Fonseca898676e2016-03-14 10:51:54 +0000265 surface(html, ref_image)
266 surface(html, src_image)
267 surface(html, delta_image)
268 html.write(' </tr>\n')
269 html.flush()
José Fonsecaf87d8c62011-02-15 12:19:11 +0000270 html.write(' </table>\n')
271 html.write(' </body>\n')
272 html.write('</html>\n')
273
Carl Worthb3b6d732012-04-03 16:02:25 -0700274 if failures:
275 sys.exit(1)
José Fonsecaf87d8c62011-02-15 12:19:11 +0000276
277if __name__ == '__main__':
278 main()