4

I am trying to compare 2 images using PIL and the below is my scenario.

img1:

img1

img2:

img2

img1 = Image.open(img1.png)
img2 = Image.open(img2.png)

I have written a simple diff function which will return -1 if there is a difference or 0 if they are same.

def diff(img1, img2):
    im1 = img1.load()
    im2 = img2.load()

    for i in range(0, img1.size[0]):
        for j in range(0, img1.size[1]):
            if(im1[i,j] != im2[i,j]):
                return -1
    return 0

I am passing the following:

diff(img2, img1.transpose(Image.FLIP_LEFT_RIGHT))

Both are exactly the same image but I get a difference. The difference seems to be at: [27 84] Can someone please explain me why?

4
  • Looks as they are not exactly the same, at least the png ones do not seem to be aligned in height. Dump them as byte stream or check them with an image toolkit. Commented Sep 9, 2017 at 4:05
  • Thanks, it looked same to a naked eye but actually there seems to be a difference. Commented Sep 9, 2017 at 12:05
  • I am running into this issue right now. What did you end up doing to solve this problem? Commented Sep 9, 2018 at 22:45
  • Welcome to KBAI. So I ended up using a similarity ratio and not just do a exact pixel by pixel comparison. Commented Sep 11, 2018 at 14:33

1 Answer 1

10

"Both are exactly the same image but I get a difference."

But they're not.

You can see this, using the code below for example:

def show_diff(img1, img2):
    diff = Image.new("RGB", img1.size, (255,255,255))
    for x1 in range(img1.size[0]):
        for y1 in range(img1.size[1]):
            x2 = img1.size[0] - 1 - x1
            y2 = img1.size[1] - 1 - y1

            if img1.getpixel((x1,y1)) != img2.getpixel((x2,y2)):
                print(x1,y1,x2,y2)
                diff.putpixel((x1,y1), (255,0,0))

    diff.show()

img_r = Image.open("img/pacman-r.png")
img_l = Image.open("img/pacman-l.png")
show_diff(img_r, img_l)

Which results in

diff

(Here, any pixel that differs between the two images is colored red.)

Or with

def show_delta(img1, img2):
    diff = Image.new("RGB", img1.size, (255,255,255))
    for x1 in range(img1.size[0]):
        for y1 in range(img1.size[1]):
            x2 = img1.size[0] - 1 - x1
            y2 = img1.size[1] - 1 - y1

            p1 = img1.getpixel((x1,y1))
            p2 = img2.getpixel((x2,y2))
            p3 = round((p1[0] / 2) - (p2[0] / 2)) + 128

            diff.putpixel((x1,y1), (p3,p3,p3))

    diff.show()

img_r = Image.open("img/pacman-r.png")
img_l = Image.open("img/pacman-l.png")
show_delta(img_r, img_l)

which results in

delta

(Here, equivalent pixels are gray while a white pixel signifies a pixel in img1 was set (dark) while unset in img2 and a black pixel signifies the opposite.)

It seems like you suspected that PIL's Image.transpose method caused the problem, but the source images aren't just transposed.

Image.transpose works as you'd expect -- so something like:

def diff(img1, img2):
    im1 = img1.load()
    im2 = img2.load()

    images_match = True
    for i in range(0, img1.size[0]):
        for j in range(0, img1.size[1]):
            if(im1[i,j] != im2[i,j]):
                images_match = False

    return images_match

img_r = Image.open("img/pacman-r.png")    
# NOTE: **NOT** Using img_l here
print(diff(img_r, img_r.transpose(Image.FLIP_LEFT_RIGHT).transpose(Image.FLIP_LEFT_RIGHT)))

returns True.

(Here, an image is compared to a twice-transposed version of itself)

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

1 Comment

Thanks a lot, let me think over your analysis and try to understand.

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.