José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ########################################################################## |
| 3 | # |
José Fonseca | cbb46fa | 2011-05-21 18:47:21 +0100 | [diff] [blame] | 4 | # Copyright 2011 Jose Fonseca |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 5 | # 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 | |
| 29 | import sys |
| 30 | import os.path |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 31 | import optparse |
José Fonseca | cbb46fa | 2011-05-21 18:47:21 +0100 | [diff] [blame] | 32 | import math |
| 33 | import operator |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 34 | |
| 35 | import Image |
José Fonseca | cbb46fa | 2011-05-21 18:47:21 +0100 | [diff] [blame] | 36 | import ImageChops |
| 37 | import ImageEnhance |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 38 | |
| 39 | |
| 40 | thumb_size = 320, 320 |
| 41 | |
| 42 | |
José Fonseca | cbb46fa | 2011-05-21 18:47:21 +0100 | [diff] [blame] | 43 | def _compare(ref_image, src_image, delta_image): |
| 44 | import subprocess |
| 45 | p = subprocess.Popen([ |
| 46 | 'compare', |
| 47 | '-metric', 'AE', |
| 48 | '-fuzz', '%u%%' % options.fuzz, |
| 49 | '-dissimilarity-threshold', '1', |
| 50 | ref_image, src_image, delta_image |
| 51 | ], stderr=subprocess.PIPE) |
| 52 | _, stderr = p.communicate() |
| 53 | try: |
| 54 | return int(stderr) |
| 55 | except ValueError: |
| 56 | return 0xffffffff |
| 57 | |
| 58 | |
| 59 | def compare(ref_image, src_image, delta_image): |
| 60 | ref_im = Image.open(ref_image) |
| 61 | src_im = Image.open(src_image) |
| 62 | |
| 63 | ref_im = ref_im.convert('RGB') |
| 64 | src_im = src_im.convert('RGB') |
| 65 | |
| 66 | diff = ImageChops.difference(src_im, ref_im) |
| 67 | |
| 68 | mask = ImageEnhance.Brightness(diff).enhance(100.0/options.fuzz) |
| 69 | mask = mask.convert('L') |
| 70 | |
| 71 | lowlight = Image.new('RGB', src_im.size, (0xff, 0xff, 0xff)) |
| 72 | highlight = Image.new('RGB', src_im.size, (0xf1, 0x00, 0x1e)) |
| 73 | delta_im = Image.composite(highlight, lowlight, mask) |
| 74 | |
| 75 | delta_im = Image.blend(src_im, delta_im, 0xcc/255.0) |
| 76 | delta_im.save(delta_image) |
| 77 | |
| 78 | # See also http://effbot.org/zone/pil-comparing-images.htm |
| 79 | # TODO: this is approximate due to the grayscale conversion |
| 80 | h = diff.convert('L').histogram() |
| 81 | ae = sum(h[255 * options.fuzz // 100 + 1 : 256]) |
| 82 | return ae |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | def surface(html, image): |
José Fonseca | 0c881f7 | 2011-05-15 11:50:01 +0100 | [diff] [blame] | 86 | if True: |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 87 | name, ext = os.path.splitext(image) |
José Fonseca | 0c881f7 | 2011-05-15 11:50:01 +0100 | [diff] [blame] | 88 | thumb = name + '.thumb' + ext |
| 89 | if os.path.exists(image) \ |
| 90 | and (not os.path.exists(thumb) \ |
| 91 | or os.path.getmtime(thumb) < os.path.getmtime(image)): |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 92 | im = Image.open(image) |
| 93 | im.thumbnail(thumb_size) |
| 94 | im.save(thumb) |
José Fonseca | 0c881f7 | 2011-05-15 11:50:01 +0100 | [diff] [blame] | 95 | else: |
| 96 | thumb = image |
| 97 | html.write(' <td><a href="%s"><img src="%s"/></a></td>\n' % (image, thumb)) |
| 98 | |
| 99 | |
| 100 | def is_image(path): |
| 101 | return \ |
| 102 | path.endswith('.png') \ |
| 103 | and not path.endswith('.diff.png') \ |
| 104 | and not path.endswith('.thumb.png') |
| 105 | |
| 106 | |
| 107 | def find_images(prefix): |
| 108 | prefix = os.path.abspath(prefix) |
| 109 | if os.path.isdir(prefix): |
| 110 | prefix_dir = prefix |
| 111 | else: |
| 112 | prefix_dir = os.path.dirname(prefix) |
| 113 | |
| 114 | images = [] |
| 115 | for dirname, dirnames, filenames in os.walk(prefix_dir, followlinks=True): |
| 116 | for filename in filenames: |
| 117 | filepath = os.path.join(dirname, filename) |
| 118 | if filepath.startswith(prefix) and is_image(filepath): |
| 119 | images.append(filepath[len(prefix):]) |
| 120 | |
| 121 | return images |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 122 | |
| 123 | |
| 124 | def main(): |
José Fonseca | cbb46fa | 2011-05-21 18:47:21 +0100 | [diff] [blame] | 125 | global options |
| 126 | |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 127 | optparser = optparse.OptionParser( |
José Fonseca | 0c881f7 | 2011-05-15 11:50:01 +0100 | [diff] [blame] | 128 | usage="\n\t%prog [options] <ref_prefix> <src_prefix>", |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 129 | version="%%prog") |
| 130 | optparser.add_option( |
| 131 | '-o', '--output', metavar='FILE', |
José Fonseca | cbb46fa | 2011-05-21 18:47:21 +0100 | [diff] [blame] | 132 | type="string", dest="output", default='index.html', |
| 133 | help="output filename [default: %default]") |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 134 | optparser.add_option( |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 135 | '-f', '--fuzz', |
José Fonseca | cbb46fa | 2011-05-21 18:47:21 +0100 | [diff] [blame] | 136 | type="int", dest="fuzz", default=5, |
| 137 | help="fuzz percentage [default: %default]") |
| 138 | optparser.add_option( |
| 139 | '--overwrite', |
| 140 | action="store_true", dest="overwrite", default=False, |
| 141 | help="overwrite") |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 142 | |
| 143 | (options, args) = optparser.parse_args(sys.argv[1:]) |
| 144 | |
| 145 | if len(args) != 2: |
| 146 | optparser.error('incorrect number of arguments') |
| 147 | |
José Fonseca | 2f6720e | 2011-04-13 15:57:15 +0100 | [diff] [blame] | 148 | ref_prefix = args[0] |
| 149 | src_prefix = args[1] |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 150 | |
José Fonseca | 0c881f7 | 2011-05-15 11:50:01 +0100 | [diff] [blame] | 151 | ref_images = find_images(ref_prefix) |
| 152 | src_images = find_images(src_prefix) |
| 153 | images = list(set(ref_images).intersection(set(src_images))) |
| 154 | images.sort() |
| 155 | |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 156 | if options.output: |
| 157 | html = open(options.output, 'wt') |
| 158 | else: |
| 159 | html = sys.stdout |
| 160 | html.write('<html>\n') |
| 161 | html.write(' <body>\n') |
| 162 | html.write(' <table border="1">\n') |
José Fonseca | 0c881f7 | 2011-05-15 11:50:01 +0100 | [diff] [blame] | 163 | html.write(' <tr><th>%s</th><th>%s</th><th>Δ</th></tr>\n' % (ref_prefix, src_prefix)) |
| 164 | for image in images: |
| 165 | ref_image = ref_prefix + image |
| 166 | src_image = src_prefix + image |
| 167 | root, ext = os.path.splitext(src_image) |
| 168 | delta_image = "%s.diff.png" % (root, ) |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 169 | if os.path.exists(ref_image) and os.path.exists(src_image): |
José Fonseca | cbb46fa | 2011-05-21 18:47:21 +0100 | [diff] [blame] | 170 | if options.overwrite \ |
| 171 | or not os.path.exists(delta_image) \ |
José Fonseca | 0c881f7 | 2011-05-15 11:50:01 +0100 | [diff] [blame] | 172 | or (os.path.getmtime(delta_image) < os.path.getmtime(ref_image) \ |
| 173 | and os.path.getmtime(delta_image) < os.path.getmtime(src_image)): |
José Fonseca | cbb46fa | 2011-05-21 18:47:21 +0100 | [diff] [blame] | 174 | compare(ref_image, src_image, delta_image) |
José Fonseca | 0c881f7 | 2011-05-15 11:50:01 +0100 | [diff] [blame] | 175 | |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 176 | html.write(' <tr>\n') |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 177 | surface(html, ref_image) |
| 178 | surface(html, src_image) |
| 179 | surface(html, delta_image) |
| 180 | html.write(' </tr>\n') |
| 181 | html.flush() |
José Fonseca | f87d8c6 | 2011-02-15 12:19:11 +0000 | [diff] [blame] | 182 | html.write(' </table>\n') |
| 183 | html.write(' </body>\n') |
| 184 | html.write('</html>\n') |
| 185 | |
| 186 | |
| 187 | if __name__ == '__main__': |
| 188 | main() |