在不同时间拍摄的相同图像在枕头中不匹配
Same image captured in different time isn't matching in pillow
我有一个程序,如果 image1 和 image2 相同,我必须退出该程序。所以,我在不同的时间截取了两个相同的屏幕截图,但它们不匹配。这是我的代码:
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")
问题是什么以及如何解决?
编辑:
我运行这段代码结果还是“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")
包含图片:
ok 重试了这两种方法,这里是代码和输出:
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")
输出:
matched
Failed
Failed
_________________________________
TRUE
False
False
在图像中似乎有问题,您可以比较枕头图像,如果转换为 numpy 数组需要使用
numpy.array_equal
比较它们
此处使用的图片(test1.png 是您的第一张图片,test1c.png 是它的副本,test2.png 是您的第二张图片,test3.png 是修改过的图片我的图片):
test1.png:
test1c.png:
test2.png:
test3.png:
如果有问题请告诉我
拍了一点,但发现图像中的像素不同:
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
这里是图片对比:
我的眼睛因眼睛疲劳而灼痛;-)
我有一个程序,如果 image1 和 image2 相同,我必须退出该程序。所以,我在不同的时间截取了两个相同的屏幕截图,但它们不匹配。这是我的代码:
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")
问题是什么以及如何解决?
编辑:
我运行这段代码结果还是“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")
包含图片:
ok 重试了这两种方法,这里是代码和输出:
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")
输出:
matched
Failed
Failed
_________________________________
TRUE
False
False
在图像中似乎有问题,您可以比较枕头图像,如果转换为 numpy 数组需要使用
numpy.array_equal
比较它们
此处使用的图片(test1.png 是您的第一张图片,test1c.png 是它的副本,test2.png 是您的第二张图片,test3.png 是修改过的图片我的图片):
test1.png:
test1c.png:
test2.png:
test3.png:
如果有问题请告诉我
拍了一点,但发现图像中的像素不同:
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
这里是图片对比:
我的眼睛因眼睛疲劳而灼痛;-)