Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

26.2. PIL

	
from PIL import Image
from PIL import ImageChops 
def compare_images(one, two, diff):
  image_one = Image.open(one)
  image_two = Image.open(two)
  try: 
    diff = ImageChops.difference(image_one, image_two)
    if diff.getbbox() is None:
      print("【+】We are the same!")
    else:
      diff.save(diff)
  except ValueError as e:
    text = ("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.")
    print("【{0}】{1}".format(e,text))
if __name__ == '__main__':
  compare_images('1.png', '2.png', '我们不一样.png')