import math, operator
from functools import reduce

def rmsdiff(im1, im2):
    """Calculate the root-mean-square difference between two images"""
h = ImageChops.difference(im1, im2).histogram()
return math.sqrt(reduce(operator.add, map(lambda h, i: h * i ** 2, h, list(range(256)))) / (float(im1.size[0]) * im1.size[1]))
python code to compare images in python