[Python] Root mean square difference between two images using Python and PIL

Discussão em 'Python' iniciado por Stack, Abril 25, 2025.

  1. Stack

    Stack Membro Participativo

    I need to have a function like the one found here: http://effbot.org/zone/pil-comparing-images.htm that calculates the root mean square difference between two images. The code looks like this:

    import ImageChops
    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()

    # calculate rms
    return math.sqrt(reduce(operator.add,
    map(lambda h, i: h*(i**2), h, range(256))
    ) / (float(im1.size[0]) * im1.size[1]))


    Trying to run this code leads to the following error:

    TypeError: unsupported operand type(s) for ** or pow(): 'NoneType' and 'int'.

    What's the matter with it?

    Continue reading...

Compartilhe esta Página