0

I would like to build a program that compares 2 images using python. I have a portion of code that will fetch the image files, I just need to find a way to take the 2 image files and compare them to see if there are any differences between the .png images.

I have attempted to make use of PIL, however, I am having issues getting the library to work with my program, I am getting the error that the _imaging C module is not installed.

It is not a very complex task, but if someone could either give me a starting point or idea, or help me make use of PIL that would be very helpful.

user1152578
  • 785
  • 1
  • 7
  • 8

2 Answers2

0

You can make a function that compares images easily using the PIL module:

from PIL import Image

def compare_images(image_file1, image_file2):
    im1 = Image.open(image_file1)
    im2 = Image.open(image_file2)
    if im1.size != im2.size:
        return False
    width, height = im1.size
    im_access1 = im1.load()
    im_access2 = im2.load()
    for i in xrange(height):
        for j in xrange(width):
            if im_access1[i,j] != im_access2[i,j]:
                return False
    return True

Of course, first you have to make your PIL module work.

juliomalegria
  • 24,229
  • 14
  • 73
  • 89
0

What is it your looking to compare within the images? The size? The metadata etc..?? If you're looking to see if they are a 100% match (creation dates, size, content etc...), I would look to obtain an MD5 hash of each image and then compare the returned hash values. This all really depends on to what level you want to compare the images. Have a look here:

Compare two images the python/linux way

Community
  • 1
  • 1
thefragileomen
  • 1,537
  • 8
  • 24
  • 40
  • I do not want the comparison to involve the time stamps, they will be different, I would like it to check to make sure that each pixel of the photos are the exact same, so yes, size, and all visual data of the images. – user1152578 Jan 31 '12 at 19:54