1.查找重复图片
利用文件的MD5值可查找完全一样的重复图片
import os,time,hashlib
def getmd5(file):
if not os.path.isfile(file):
return
fd = open(file,'rb')
md5 = hashlib.md5()
md5.update(fd.read())
fd.close()
return md5.hexdigest()
if __name__ == "__main__":
allfile = []
md5list = []
list_delete = []
start = time.time()
path = 'D:\\_PLAY\\新建文件夹\\'
for filepath,dir,filelist in os.walk(path):
for filename in filelist:
allfile.append(os.path.join(filepath,filename))
#根据MD5值比较
for photo in allfile:
md5sum = getmd5(photo)
if md5sum not in md5list:
md5list.append(md5sum)
else:
list_delete.append(photo)
print('重复的照片有:',list_delete)
#删除图片
for i in range(len(list_delete)):
os.remove(list_delete[i])
二.查找相似图片
本文采用四种哈希值进行比较:
1.感知哈希(perception hashing)
2.平均散列(average hashing)
3.梯度散列(difference hashing)
4.离散小波变换(wavelet hashing)
import os,imagehash
from PIL import Image
def hash(path):
highfreq_factor = 4 # resize的尺度
hash_size = 32 # 最终返回hash数值长度
image_scale = 64
img_size = hash_size * highfreq_factor
list_file = []
list_phash = []
list_ahash = []
list_dhash = []
list_whash = []
for file in os.listdir(path):
if os.path.splitext(file)[1] == '.jpg':
path_file = os.path.join(path, file) # 拼路径
list_file.append(file)
phash = imagehash.phash(Image.open(path_file),hash_size=hash_size,highfreq_factor=highfreq_factor)#感知哈希(perception hashing)
ahash = imagehash.average_hash(Image.open(path_file),hash_size=hash_size)#平均散列(average hashing)
dhash = imagehash.dhash(Image.open(path_file),hash_size=hash_size)#梯度散列(difference hashing)
whash = imagehash.whash(Image.open(path_file),image_scale=image_scale,hash_size=hash_size,mode = 'db4')#离散小波变换(wavelet hashing)
list_phash.append(phash)
list_ahash.append(ahash)
list_dhash.append(dhash)
list_whash.append(whash)
#print(list_hash)
for i in range(len(list_file)):
for j in range(i+1,len(list_file)):
phash_value = 1-(list_phash[i]-list_phash[j])/len(list_phash[i].hash)**2
ahash_value = 1-(list_ahash[i]-list_ahash[j])/len(list_ahash[i].hash)**2
dhash_value = 1-(list_dhash[i]-list_dhash[j])/len(list_dhash[i].hash)**2
whash_value = 1-(list_whash[i]-list_whash[j])/len(list_whash[i].hash)**2
value_hash = max(phash_value,ahash_value,dhash_value,whash_value)
if(value_hash > 0.9):#阈值设为0.9
size_i = os.path.getsize(path + '\\' + list_file[i])
size_j = os.path.getsize(path + '\\' + list_file[j])
print(list_file[i],str(size_i/1024)+'KB')
print(list_file[j],str(size_j/1024)+'KB')
print(value_hash)
print('***********************')
if __name__ == '__main__':
path = r'D:\_PLAY\新建文件夹\Excel\ALL_Daily'
hash(path)