4"""Compares pairs of page images and generates an HTML to look at differences.
16from common
import DirectoryFinder
20 return image_comparison.GenerateOneDiff(image)
24 """Compares pairs of page images and generates an HTML to look at differences.
26 The images are all assumed to have the same name and be in two directories:
27 [output_path]/[two_labels[0]] and [output_path]/[two_labels[1]]. For example,
28 if output_path is "/tmp/images" and two_labels is ("before", "after"),
29 images in /tmp/images/before will be compared to /tmp/images/after. The HTML
30 produced will be in /tmp/images/compare.html and have relative links to these
31 images, so /tmp/images is self-contained and can be moved around or shared.
34 def __init__(self, build_dir, output_path, two_labels, num_workers,
39 build_dir: Path to the build directory.
40 output_path: Path with the pngs and where the html will be created.
41 two_labels: Tuple of two strings that name the subdirectories in
42 output_path containing the images.
43 num_workers: Number of worker threads to start.
44 threshold_fraction: Minimum percentage (0.0 to 1.0) of pixels below which
45 an image is considered to have only small changes. They will not be
46 displayed on the HTML, only listed.
54 def Run(self, open_in_browser):
55 """Runs the comparison and generates an HTML with the results.
65 print(
'two_labels must be a tuple of length 2', file=sys.stderr)
71 html_path = os.path.join(self.
output_path,
'compare.html')
84 with open(html_path,
'w')
as f:
85 f.write(
'<html><body>')
88 diff = difference[image]
90 print(
'Failed to compare image %s' % image, file=sys.stderr)
94 small_changes.append((image, diff))
97 f.write(
'</body></html>')
100 webbrowser.open(html_path)
105 """Runs a diff over all pairs of page images, producing diff images.
107 As a side effect, the diff images will be saved to [output_path]/diff
108 with the same image name.
111 A dict mapping image names to percentage of pixels changes.
115 worker_func = functools.partial(GenerateOneDiffParallel, self)
120 one_year_in_seconds = 3600 * 24 * 365
125 for worker_result
in worker_results:
126 image, result = worker_result
127 difference[image] = result
128 except KeyboardInterrupt:
139 """Runs a diff over one pair of images, producing a diff image.
141 As a side effect, the diff image will be saved to [output_path]/diff
142 with the same image name.
145 image: Page image to compare.
148 A tuple (image, diff), where image is the parameter and diff is the
149 percentage of pixels changed.
152 subprocess.check_output([
157 except subprocess.CalledProcessError
as e:
158 percentage_change = float(re.findall(
r'\d+\.\d+', e.output)[0])
163 subprocess.check_output([
169 except subprocess.CalledProcessError
as e:
170 return image, percentage_change
172 print(
'Warning: Should have failed the previous diff.', file=sys.stderr)
176 return os.path.relpath(absolute_path, start=self.
output_path)
179 """Write table rows for a page image comparing its two versions.
182 f: Open HTML file to write to.
183 image: Image file name.
184 diff: Percentage of different pixels.
186 f.write(
'<tr><td colspan="2">')
187 f.write(
'%s (%.4f%% changed)' % (image, diff))
188 f.write(
'</td></tr>')
199 """Write table column with a single image.
202 f: Open HTML file to write to.
203 image_path: Path to image file.
206 f.write(
'<img src="%s">' % image_path)
210 """Write table column for an image comparing its two versions.
213 f: Open HTML file to write to.
214 normal_image_path: Path to image to be used in the "normal" state.
215 hover_image_path: Path to image to be used in the "hover" state.
218 f.write(
'<img src="%s" '
219 'onmouseover="this.src=\'%s\';" '
220 'onmouseout="this.src=\'%s\';">' %
221 (normal_image_path, hover_image_path, normal_image_path))
225 """Write table rows for all images considered to have only small changes.
228 f: Open HTML file to write to.
229 small_changes: List of (image, change) tuples, where image is the page
230 image and change is the percentage of pixels changed.
232 for image, change
in small_changes:
233 f.write(
'<tr><td colspan="2">')
235 f.write(
'No change for: %s' % image)
237 f.write(
'Small change of %.4f%% for: %s' % (change, image))
238 f.write(
'</td></tr>')
242 """Contains the locations of input and output image files.
245 def __init__(self, output_path, diff_path, two_labels):
249 output_path: Path to directory with the pngs.
250 diff_path: Path to directory where the diffs will be generated.
251 two_labels: Tuple of two strings that name the subdirectories in
252 output_path containing the images.
265 pieces = s.rsplit(
'.', 2)
266 return (pieces[0], int(pieces[1]))
268 self.
images.sort(key=KeyFn)
274 """Traverses a dir and builds a dict of all page images to compare in it.
277 label: name of subdirectory of output_path to traverse.
280 Dict mapping page image names to the path of the image file.
282 image_path_matcher = os.path.join(self.
output_path, label,
'*.*.png')
283 image_paths = glob.glob(image_path_matcher)
286 os.path.split(image_path)[1]: image_path
for image_path
in image_paths
292 """Returns a list of all page images present in both directories."""
296 """Returns the path for a page image in the first subdirectory."""
297 return self.
left[test_case]
300 """Returns the path for a page image in the second subdirectory."""
301 return self.
right[test_case]
304 """Returns the path for a page diff image."""
305 return self.
diff[test_case]
static QDBusError::ErrorType get(const char *name)
QDebug print(QDebug debug, QSslError::SslError error)
file open(QIODevice::ReadOnly)