比较两个图像python / linux的方式(Compare two images the python/linux way)
试图解决防止重复图像上传的问题。
我有两个JPG。 看着它们,我可以看到它们实际上是相同的。 但由于某些原因,它们具有不同的文件大小(一个是从备份中提取的,另一个是另一个上传文件),因此它们具有不同的md5校验和。
我如何有效和自信地比较两幅图像,就像人类能够看到它们明显相同一样?
更新
我写了这个脚本:
import math, operator
from PIL import Image
def compare(file1, file2):
image1 = Image.open(file1)
image2 = Image.open(file2)
h1 = image1.histogram()
h2 = image2.histogram()
rms = math.sqrt(reduce(operator.add,
map(lambda a,b: (a-b)**2, h1, h2))/len(h1))
return rms
if __name__=='__main__':
import sys
file1, file2 = sys.argv[1:]
print compare(file1, file2)
然后我下载了两张视觉相同的图像并运行脚本。 输出:
58.9830484122
任何人都可以告诉我什么是合适的截止点?
更新II
a.jpg和b.jpg的区别在于第二个已经保存了PIL:
b=Image.open('a.jpg')
b.save(open('b.jpg','wb'))
这显然适用于一些非常非常轻质的修改。 现在我已经解决了我的问题,通过将相同的PIL保存到正在上传的文件中,而无需执行任何操作,现在它可以工作!
Trying to solve a problem of preventing duplicate images to be uploaded.
I have two JPGs. Looking at them I can see that they are in fact identical. But fo