安裝:
pip install Pillow
pip install ImageHash
創(chuàng)建索引庫(kù):
def createDatabase(path):
db = shelve.open(dataPath, writeback=True)
pool = glob.glob(path+'*.jpg')+glob.glob(path+'*.png')
for index, imagePath in enumerate(pool):
image = Image.open(imagePath)
h = str(imagehash.dhash(image))
filename = imagePath[imagePath.rfind('/')+1:]
try:
findFiles = db[h]
except:
findFiles = None
if findFiles!=None:
continue
else:
image = Image.open(imagePath)
image.show()
db[h] = db.get(h,[])+[filename]
print(index)
db.close()
查相同圖片:
def checkSame(path):
db = shelve.open(dataPath, writeback=False)
pool = glob.glob(path+'*.jpg')+glob.glob(path+'*.png')
for index, imagePath in enumerate(pool):
query = Image.open(imagePath)
h = str(imagehash.dhash(query))
filename = imagePath[imagePath.rfind('/')+1:]
if db[h]:
image = Image.open(imagePath)
image.show()
image = Image.open(imgPath+val[0])
image.show()
db.close()
查相似:
相似度查詢:1 - (hash1 - hash2)/len(hash1.hash)**2
蔬捷,當(dāng)兩個(gè)圖片越相似垄提,此公式得到的值越趨近于0
def checkSimilar(path):
db = shelve.open(dataPath, writeback=False)
pool = glob.glob(path+'*.jpg')+glob.glob(path+'*.png')
for index, imagePath in enumerate(pool):
query = Image.open(imagePath)
h = str(imagehash.dhash(query))
filename = imagePath[imagePath.rfind('/')+1:]
for key,val in db.items():
h1 = imagehash.hex_to_hash(key)
h2 = imagehash.hex_to_hash(h)
similar = (h1 - h2)/len(h1.hash)**2
if similar<.1 and val[0]!=filename:
image = Image.open(imagePath)
image.show()
image = Image.open(imgPath+val[0])
image.show()
db.close()
參考:
shelve ,image hash 周拐,blog