import Image
i1 = Image.open("image1.jpg")
i2 = Image.open("image2.jpg")
pairs = zip(i1.getdata(), i2.getdata())
if len(i1.getbands()) == 1:
# for gray-scale jpegs
dif = sum(abs(p1-p2) for p1,p2 in pairs)
else:
dif = sum(abs(c1-c2) for p1,p2 in pairs for c1,c2 in zip(p1,p2))
ncomponents = i1.size[0] * i1.size[1] * 3
print "Difference (percentage):", (dif / 255.0 * 100) / ncomponents
上面的代码是要获取2张图片之间的百分比差异? 如果每个像素值相减,除以255的必要性是什么?