Warm tip: This article is reproduced from serverfault.com, please click

Wand/ImageMagick compare method always returns same float number

发布于 2020-11-30 21:38:10

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)
Questioner
Mojimi
Viewed
0
fmw42 2020-12-01 07:07:07

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