本文实例讲述了Python比较两个图片相似度的方法。分享给大家供大家参考。具体分析如下:
这段代码实用pil模块比较两个图片的相似度,根据实际实用,代码虽短但效果不错,还是非常靠谱的,前提是图片要大一些,太小的图片不好比较。附件提供完整测试代码和对比用的图片。
# Filename: histsimilar.py
# -*- coding: utf-8 -*-
import Image
def make_regalur_image(img, size = (256, 256)):
return img.resize(size).convert('RGB')
def split_image(img, part_size = (64, 64)):
w, h = img.size
pw, ph = part_size
assert w % pw == h % ph == 0
return [img.crop((i, j, i+pw, j+ph)).copy() \
for i in xrange(0, w, pw) \
for j in xrange(0, h, ph)]
def hist_similar(lh, rh):
assert len(lh) == len(rh)
return sum(1 - (0 if l == r else float(abs(l - r))/max(l, r)) for l, r in zip(lh, rh))/len(lh)
def calc_similar(li, ri):
# return hist_similar(li.histogram(), ri.histogram())
return sum(hist_similar(l.histogram(), r.histogram()) for l, r in zip(split_image(li), split_image(ri))) / 16.0
def calc_similar_by_path(lf, rf):
li, ri = make_regalur_image(Image.open(lf)), make_regalur_image(Image.open(rf))
return calc_similar(li, ri)
def make_doc_data(lf, rf):
li, ri = make_regalur_image(Image.open(lf)), make_regalur_image(Image.open(rf))
li.save(lf + '_regalur.png')
ri.save(rf + '_regalur.png')
fd = open('stat.csv', 'w')
fd.write('\n'.join(l + ',' + r for l, r in zip(map(str, li.histogram()), map(str, ri.histogram()))))
# print >>fd, '\n'
# fd.write(','.join(map(str, ri.histogram())))
fd.close()
import ImageDraw
li = li.convert('RGB')
draw = ImageDraw.Draw(li)
for i in xrange(0, 256, 64):
draw.line((0, i, 256, i), fill = '#ff0000')
draw.line((i, 0, i, 256), fill = '#ff0000')
li.save(lf + '_lines.png')
if __name__ == '__main__':
path = r'testpic/TEST%d/%d.JPG'
for i in xrange(1, 7):
print 'test_case_%d: %.3f%%'%(i, \
calc_similar_by_path('testpic/TEST%d/%d.JPG'%(i, 1), 'testpic/TEST%d/%d.JPG'%(i, 2))*100)
# make_doc_data('test/TEST4/1.JPG', 'test/TEST4/2.JPG')
完整实例代码点击此处本站下载。
希望本文所述对大家的Python程序设计有所帮助。