-1

I have a program where I have to exit the program if image1 and image2 are the same. So, I took two same screenshot but in different time, but they aren't matching. Here is my code:

import pyautogui
import time
from PIL import Image

time.sleep(3)




image1 = Image.open('my_screenshot0.png')
image2 = Image.open('my_screenshot1.png')


if image1 == image2:
    print("matched")
else:
    print("Failed")

what is the problem and how to solve it ?

Edit:

I ran this code and the result is still "False"..

import pyautogui
import numpy as np


pyautogui.screenshot('test1.png')
pyautogui.screenshot('test2.png')

if np.array('test1.png') == np.array('test2.png'):
    print('TRUE')
else:
    print("False")

Images are included:

test1

test2

  • This has been already answered in https://stackoverflow.com/questions/35176639/compare-images-python-pil, check it and come back if its not working – Ghost Ops Aug 19 '21 at 13:21
  • How can you be sure that the images are exactly equal? To be sure try comparing the pixel values as lumpy arrays `np.array(image1) == np.array(image2)` – Matteo Zanoni Aug 19 '21 at 13:24
  • @GhostOps Nah, I tried that but not working,, – Asir Shahriar Roudra Aug 19 '21 at 13:30
  • @MatteoZanoni i am sure because i used pyautogui to take the screenshots and compare at the same time – Asir Shahriar Roudra Aug 19 '21 at 13:35
  • Hopefully there was no moving video, no clock and no mouse in your images... share the images and we will be able to tell you the difference. – Mark Setchell Aug 19 '21 at 13:38
  • @MarkSetchell I've added the images. – Asir Shahriar Roudra Aug 19 '21 at 13:50
  • Maybe the problem is that the object must be the exact same, because both these images take up different areas in memory, so I think that's the problem. – LuckElixir Aug 19 '21 at 13:57
  • 1
    The difference is in the *"cloud sync"* icon immediately to the left of the clock at bottom right. You can find it immediately with **ImageMagick** in Terminal with `magick a.png b.png -compose difference -composite result.png` – Mark Setchell Aug 19 '21 at 13:58
  • @MarkSetchell I resolved the cloud sync , but the problem again appears , check the result and test1, test2 pictures. link: https://imgur.com/a/Fm9JOyx – Asir Shahriar Roudra Aug 19 '21 at 14:24
  • There's a difference in the vertical bar down the right side of the images, try emphasising the difference with `magick hkdD8Qe.png SK9XVz9.png -compose difference -composite -normalize result.png` – Mark Setchell Aug 19 '21 at 14:28
  • 1
    Does this answer your question? [Compare images Python PIL](https://stackoverflow.com/questions/35176639/compare-images-python-pil) – pippo1980 Aug 19 '21 at 23:46

1 Answers1

2

ok re-tried both approaches, here code and output:


from PIL import Image

import numpy as np


image1 = Image.open('test1.png')
image1c = Image.open('test1c.png')
image2 = Image.open('test2.png')
image3 = Image.open('test3.png')

if image1 == image1c:
    print("matched")
else:
    print("Failed")

if image1 == image2:
    print("matched")
else:
    print("Failed")
    
if image1 == image3:
    print("matched")
else:
    print("Failed")
    
print('_________________________________')


img1 = np.array(image1)
img1c = np.array(image1c)
img2 = np.array(image2)
img3 = np.array(image3)

if np.array_equal(img1,img1c) : 
    print('TRUE')
else:
    print("False")
    
if np.array_equal(img1,img2) : 
    print('TRUE')
else:
    print("False")
    
if np.array_equal(img1,img3) : 
    print('TRUE')
else:
    print("False")

output:

matched
Failed
Failed
_________________________________
TRUE
False
False

so seems problem where in images, You can compare pillow Images and if converted to numpy arrays need to use

numpy.array_equal

to compare them

here the images used (test1.png is your first image, test1c.png its a copy of it, test2.png is your second image, test3.png its a modified by me image):

test1.png: test1.png

test1c.png: test1c.png:

test2.png: test2.png:

test3.png: test3.png:

let me know if something is wrong with it

took a little bit , but found different pixels in images:

1749 1051
1750 1051
1751 1051
1752 1051
1753 1051
1755 1051
1748 1052
1749 1052
1750 1052
1751 1052
1752 1052
1753 1052
1755 1052
1756 1052
1748 1053
1749 1053
1750 1053
1753 1053
1754 1053
1755 1053
1752 1054
1753 1054
1753 1055
1754 1055
1755 1055
1752 1056
1754 1056
1755 1056
1757 1056
1750 1057
1751 1057
1752 1057
1753 1057
1754 1057
1757 1057
1758 1057
1759 1057
1751 1058
1752 1058
1754 1058
1755 1058
1756 1058
1757 1058
1758 1058
1754 1059
1755 1059
1756 1059
1757 1059

here the pics comparison:

differences

my eyes are burning from eye strain ;-)

pippo1980
  • 2,181
  • 3
  • 14
  • 30