Python 通过PIL比较两个图片的不同
这几天的一个比赛做一个misc时所用到的脚本,虽然最后没做出来
主要功能就是检查两个图片的不同,并同时生成一个不同的图片
from PIL import Image
from PIL import ImageChops
def compare_images(path_one, path_two, diff_save_location):#文件1的路径,文件2的路径,生成比较后不同的图片文件路径
image_one = Image.open(path_one)
image_two = Image.open(path_two)
try:
diff = ImageChops.difference(image_one, image_two)
if diff.getbbox() is None:
# 图片间没有任何不同则直接退出
print("We are the same!")
else:
diff.save(diff_save_location)
except ValueError as e:
text = ("表示图片大小和box对应的宽度不一致,参考API说明:Pastes another image into this image."
"The box argument is either a 2-tuple giving the upper left corner, a 4-tuple defining the left, upper, "
"right, and lower pixel coordinate, or None (same as (0, 0)). If a 4-tuple is given, the size of the pasted "
"image must match the size of the region.使用2纬的box避免上述问题")
print("【{0}】{1}".format(e,text))
if __name__ == '__main__':
compare_images('empire_P1.bmp','empire_P2.bmp','diff.bmp')