Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix perceptualdiff #82

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions docs/index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ generate a diff PNG file when a test fails, highlighting the differences
between the baseline image and the new screenshot.

Note that to use the PerceptualDiff engine you will first need to
`download <http://pdiff.sourceforge.net/>`_ the ``perceptualdiff`` binary and
place it in your ``PATH``.
`download <https://github.com/myint/perceptualdiff/releases>`_,
`build and install <https://github.com/myint/perceptualdiff#build-instructions>`_
PerceptualDiff or otherwise ensure that the ``perceptualdiff`` binary is in
your ``PATH``.

To use the ImageMagick engine you will need to install a package on your
machine (e.g. ``sudo apt-get install imagemagick`` on Ubuntu or
Expand Down
28 changes: 12 additions & 16 deletions needle/engines/perceptualdiff_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,26 @@ def assertSameFiles(self, output_file, baseline_file, threshold):
width, height = Image.open(output_file).size
threshold = int(width * height * threshold)

diff_ppm = output_file.replace(".png", ".diff.ppm")
cmd = "%s -threshold %d -output %s %s %s" % (
self.perceptualdiff_path, threshold, diff_ppm, baseline_file, output_file)
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if self.perceptualdiff_output_png:
diff_file = output_file.replace(".png", ".diff.png")
else:
diff_file = output_file.replace(".png", ".diff.ppm")
cmd = [self.perceptualdiff_path, '-threshold', str(threshold), '-output', diff_file, baseline_file, output_file]
process = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
perceptualdiff_stdout, _ = process.communicate()

# Sometimes perceptualdiff returns a false positive with this exact message:
# 'FAIL: Images are visibly different\n0 pixels are different\n\n'
# We catch that here.
if process.returncode == 0 or b'\n0 pixels are different' in perceptualdiff_stdout:
# No differences found, but make sure to clean up the .ppm in case it was created.
if os.path.exists(diff_ppm):
os.remove(diff_ppm)
# No differences found, but make sure to clean up the diff file in
# case it was created.
if os.path.exists(diff_file):
os.remove(diff_file)
return
else:
if os.path.exists(diff_ppm):
if self.perceptualdiff_output_png:
# Convert the .ppm output to .png
diff_png = diff_ppm.replace("diff.ppm", "diff.png")
Image.open(diff_ppm).save(diff_png)
os.remove(diff_ppm)
diff_file_msg = ' (See %s)' % diff_png
else:
diff_file_msg = ' (See %s)' % diff_ppm
if os.path.exists(diff_file):
diff_file_msg = ' (See %s)' % diff_file
else:
diff_file_msg = ''
raise AssertionError("The new screenshot '%s' did not match "
Expand Down