Add algorithms/distance/rmsdiff.py

This commit is contained in:
Benjamin Loison 2024-03-22 13:59:41 +01:00
parent cf22ff2694
commit 72e37a252a
Signed by: Benjamin_Loison
SSH Key Fingerprint: SHA256:BtnEgYTlHdOg1u+RmYcDE0mnfz1rhv5dSbQ2gyxW8B8

View File

@ -0,0 +1,14 @@
from PIL import ImageChops
import math
import operator
import functools
def rmsdiff(im1, im2):
"Calculate the root-mean-square difference between two images"
h = ImageChops.difference(im1, im2).histogram()
# calculate rms
return math.sqrt(functools.reduce(operator.add,
map(lambda h, i: h*(i**2), h, range(256))
) / (float(im1.size[0]) * im1.size[1]))