33

I'm playing around with PIL and came across this problem and I can't see where in the docs I'm going wrong. Here is my simple code

from PIL import Image
from PIL.ImageChops import difference

imageA = Image.open("image1.png")
imageB = Image.open("image2.png")

if imageA.size == imageB.size:
    diff = difference(imageA, imageB)
    diff.save("test.png")

which gives me the error

Traceback (most recent call last):
  File "C:\[XXX]\box-test.py", line 8, in <module>
    diff = difference(imageA, imageB)
  File "C:\Python32\lib\site-packages\PIL\ImageChops.py", line 123, in difference
    return image1._new(image1.im.chop_difference(image2.im))
ValueError: images do not match

Any help would be appreciated

3
  • 9
    Is imageA.mode == imageB.mode? Commented Sep 6, 2012 at 1:09
  • 1
    Looks like it was a problem with the image modes, they had to be the same! Commented Sep 6, 2012 at 1:51
  • Has image1.png the same dimensions as image2.png ? Commented Sep 6, 2012 at 9:40

3 Answers 3

53

The documentation for this function doesn't tell much in fact. So let me try to clarify it a little. First, the sizes of the images are irrelevant to whether the function works or not, it internally checks for a size that both images fit.

Now, when can you actually compare the images by using the function ImageChops.difference ?

First, both images have to have pixels that can be stored in an unsigned byte. This is a very common type of image, but this excludes comparison between images even if they are the same mode. So, you cannot compare an image x and y when one or /both/ of them have a mode of: F, I, I;16, I;16L, I;16B, BGR;15, BGR;16, BGR;24, or BGR;32. Just to make it clear: it doesn't matter if both images are in the same mode if they happen to be in one of the modes above, the function will refuse to work.

So, the comparison can be done when the images are in the modes 1, P, L, LA, RGB, RGBA, RGBX, RGBa, CMYK, or YCbCr as long as they have the same number of bands. This means the images don't have to have the same mode to be compared. For instance, difference(x.convert('CMYK'), x.convert('RGBA')) or difference(x.convert('1'), x.convert('P')) work just fine. Of course this means difference(x.convert('LA'), x.convert('L')), fails. Finally, the resulting image will always have the mode equal to the first image passed to the function.

This is valid at least for the PIL 1.1.7.

Sign up to request clarification or add additional context in comments.

Comments

2

just verify the size of each image ( for example 1024 x 1024) for each layer and image

Comments

-1

Things have progressed since this was originally posted. The best way to compare images is with hashing the image. Do not use Cryptographic hashes like MD5, they amplify differences.

There are hashes designed to find images that are similar. The one I have found most useful is pHash (perceptual hash). Check out the Python library ImageHash, which has been reasonably accurate for me.

After you hash the two images you will have to compare the hashes. There are number of libraries for finding duplicate images available. You might want to check out Perception.

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
Assumes the OP is asking a different question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.