Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python PIL image compare issue

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?

like image 306
Venkat .Tarakad Avatar asked Jan 03 '23 12:01

Venkat .Tarakad


1 Answers

"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)

like image 83
jedwards Avatar answered Jan 06 '23 00:01

jedwards