8000 Add support for image diff in headless chrome by mattpap · Pull Request #6595 · bokeh/bokeh · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Compute image diff even if image dimensions differ
  • Loading branch information
mattpap committed Mar 25, 2018
commit d161a418f80170deb418c0bd4c910db580e49a0f
4 changes: 0 additions & 4 deletions tests/examples/collect_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,6 @@ def upload_imgs(self):
def images_differ(self):
return self.pixels != 0

@property
def dimensions_differ(self):
return self.pixels == -1

def add_examples(list_of_examples, path, examples_dir, example_type=None, slow=None, skip=None, no_js=None, no_diff=None):
if path == '*':
example_path = examples_dir
Expand Down
4 changes: 1 addition & 3 deletions tests/examples/examples_report.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@
{% if not skipped and not example.no_js %}
<a href="{{ example.img_path_or_url }}"><img class="thumbnail" src="{{ example.img_path_or_url }}"></img></a>
{% if not example.no_diff %}
{% if example.dimensions_differ %}
<div class="thumbnail">Dimensions differ</div>
{% elif example.images_differ %}
{% if example.images_differ %}
<a href="{{ example.diff_path_or_url }}"><img class="thumbnail" src="{{ example.diff_path_or_url }}"></img></a>
{% else %}
<div class="thumbnail">No diff</div>
Expand Down
2 changes: 1 addition & 1 deletion tests/examples/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def _get_pdiff(example):

example.pixels = image_diff(diff_path, img_path, ref_path)
if example.pixels != 0:
comment = "dimensions don't match" if example.pixels == -1 else white("%.02f%%" % example.pixels) + " of pixels"
comment = white("%.02f%%" % example.pixels) + " of pixels"
warn("generated and reference images differ: %s" % comment)
else:
ok("generated and reference images match")
Expand Down
21 changes: 13 additions & 8 deletions tests/plugins/image_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@
logging.getLogger('PIL.PngImagePlugin').setLevel(logging.INFO)

def image_diff(diff_path, before_path, after_path, superimpose=False):
""" Returns the percentage of differing pixels or -1 if dimensions differ. """
""" Returns the percentage of differing pixels. """
before = Image.open(before_path)
after = Image.open(after_path)

if before.size != after.size:
return -1

before = before.convert('RGBA')
after = after.convert('RGBA')

mask = ImageChops.difference(before, after)
width = max(before.width, after.width)
height = max(before.height, after.height)

resized_before = Image.new("RGBA", (width, height), "white")
resized_after = Image.new("RGBA", (width, height), "white")

resized_before.paste(before)
resized_after.paste(after)

mask = ImageChops.difference(resized_before, resized_after)
mask = mask.convert('L')
mask = mask.point(lambda k: 0 if k == 0 else 255)

Expand All @@ -23,16 +29,15 @@ def image_diff(diff_path, before_path, after_path, superimpose=False):
else:
diff = mask.convert('RGB')
if superimpose:
diff.paste(after, mask=mask)
diff.paste(resized_after, mask=mask)
else:
diff.paste((0, 0, 255), mask=mask)
diff.save(diff_path)

w, h = after.size
pixels = 0

for v in mask.getdata():
if v == 255:
pixels += 1

return float(pixels)/(w*h)*100
return float(pixels)/(width*height)*100
0