python对比两个图片的差异并记录

1
2
3
4
5
6
7
8
9
10
11
from PIL import Image, ImageChops, ImageStat
im1 = Image.open("a.jpg")
im2 = Image.open("b.jpg")
diff = ImageChops.difference(im1, im2)
diff.show()
diff.save("c.jpg")
stat = ImageStat.Stat(diff)
sum_channel_values = sum(stat.mean)
max_all_channels = len(stat.mean) * 100
diff_ratio = sum_channel_values/max_all_channels
print(diff_ratio)