0

The documentation for Wand, says that the Image.compare method returns a tuple containing the difference image and a integral number representating the difference between these.

The problem is that this number always returns a float with the value of 0.6231802821079919, even if the images are the same!

I just want to get a value that tells me the percentage that an image differs from another.

This is my code. Its actually comparing PDFs but converts each page to images, just input the same pdf in both parameters to see that it returns 0.6231802821079919.

What do I do with this number?

from wand.image import Image

def get_pdf_difference(control_pdf, test_pdf):
    with Image(filename=control_pdf, format='pdf') as control:
        with Image(filename=test_pdf, format='pdf') as test:
            control.format = 'png'
            test.format = 'png'
            if len(control.sequence) != len(test.sequence):
                raise AttributeError('PDFs are of different length')
            final_diff = 0
            for i, control_page in enumerate(control.sequence):
                test_page = test.sequence[i]
                #Docs gives an example doing this but not changing anything
                #control_page.fuzz = control_page.quantum_range * 0.20
                result_image, result_metric = control_page.compare(test_page)
                final_diff += result_metric
            return final_diff/len(control.sequence)

diff = get_pdf_difference('test.pdf', 'test.pdf')
print(diff)
Mojimi
  • 2,561
  • 9
  • 52
  • 116
  • Looks like the documentation is out of date. Method should return a **Real** number as a `result_metric`, not an integral. As @fmw42 pointed out in his great answer, you need to define which metric to calculate. See [COMPARE_METRICS](https://docs.wand-py.org/en/0.6.5/wand/image.html#wand.image.COMPARE_METRICS) – emcconville Dec 01 '20 at 01:43

1 Answers1

3

Here is a simple example in Python Wand. It will list the rmse difference and show an image where the difference is highlighted in red.

Input 1:

enter image description here

Input 2:

enter image description here

from wand.image import Image
from wand.display import display

with Image(filename='img1.jpg') as bimg:
    with Image(filename='img2.jpg') as fimg:
        bimg.fuzz = 0.25*bimg.quantum_range
        bimg.artifacts['compare:highlight-color'] = 'red'
        bimg.artifacts['compare:lowlight-color'] = 'transparent'
        diff_img, diff_val =  bimg.compare(fimg, 'root_mean_square')
        print(diff_val)
        with diff_img:
            diff_img.save(filename='img1_img2_diff.jpg')
            display(diff_img)

RMSE difference:
0.0238380675979382

Difference Image:

enter image description here


Now repeat with the same Image 1.

from wand.image import Image
from wand.display import display

with Image(filename='img1.jpg') as bimg:
    with Image(filename='img1.jpg') as fimg:
        bimg.fuzz = 0.25*bimg.quantum_range
        bimg.artifacts['compare:highlight-color'] = 'red'
        bimg.artifacts['compare:lowlight-color'] = 'transparent'
        diff_img, diff_val =  bimg.compare(fimg, 'root_mean_square')
        print(diff_val)
        with diff_img:
            diff_img.save(filename='img1_img2_diff.jpg')
            display(diff_img)

RMSE difference:

0.0

fmw42
  • 46,825
  • 10
  • 62
  • 80
  • Thank you, seems like doc is out of date like @emcconville said, So a 0.02 RMSE difference means that the image is about 2% different? – Mojimi Dec 01 '20 at 12:37