我正在创建一个简单的脚本,用于检查图像是否相同或不同。
我的代码适用于jpg文件,但不适用于png文件。
由于某些原因,我的下面的代码认为下面的png图像:
与下面的png图像相同
from PIL import Image, ImageChops
img1 = Image.open('./1.png')
img2 = Image.open('./2.png')
img3 = Image.open('./A.jpg')
img4 = Image.open('./B.jpg')
diff1 = ImageChops.difference(img3, img4)
diff = ImageChops.difference(img2, img1)
print(diff.getbbox())
if diff.getbbox():
diff.show() # does not work for me. should show image if they are different
print(diff1.getbbox())
if diff1.getbbox():
diff1.show() # this works not sure why the PNG files do not
我在Ubuntu上运行这个程序。不确定我到底做错了什么。非常感谢任何帮助!
@Mark的帮助下,代码现在可以正常工作:https://github.com/timothy/image_diff/blob/master/test.py