# by movie on 2019/12/18 from PIL import Image from PIL import ImageChops path1 = 'images/trumpA689.jpg' path2 = 'images/trumpA748.jpg' diff_path = 'images/diff.bmp' imageA = Image.open('images/ImageA.bmp') imageB = Image.open('images/ImageB.bmp') dif = ImageChops.difference(imageB, imageA).getbbox() print(dif) # draw = ImageDraw.Draw(imageA) # # draw.rectangle(dif) # imageA.show() def compare(path1, path2, diffpath): path1 = Image.open(path1) path2 = Image.open(path2) difference = ImageChops.difference(path1, path2) if difference.getbbox(): difference.save(diffpath) compare(path1, path2, diff_path)
注意:要比较的两张图片大小要一样
参考: https://www.blog.pythonlibrary.org/2016/10/11/how-to-create-a-diff-of-an-image-in-python/