当前位置: 首页 > 图灵资讯 > 行业资讯> python中如何删除相似的图片?

python中如何删除相似的图片?

发布时间:2026-01-29 21:49:16

方法一:相邻两个文件相似。如果相似,将第二个添加到新列表中,然后将新列表重新删除。

例如,如果有文件1-10,首先将1和2进行比较。如果相似,将2添加到新列表中,然后将2和3进行比较。如果不相似,则继续进行3和4的比较...直到最后,删除新列表中的图片

代码如下:

#!/usr/bin/envpython
#-*-coding:utf-8-*-
#@Time:2019/1/159:19
#@Author:xiaodai
importos
importcv2
fromskimage.measureimportcompare_ssim
#importshutil
#defyidong(filename1,filename2):
#shutil.move(filename1,filename2)
defdelete(filename1):
os.remove(filename1)
if__name__='__main__':
path=r'D:\camera_pic\test\rec_pic'
#save_path_img=r'E:\0115_test\rec_pic'
#os.makedirs(save_path_img,exist_ok=True)
img_path=path
imgs_n=[]
num=[]
img_files=[os.path.join(rootdir,file)forrootdir,_,filesinos.walk(path)forfileinfilesif
(file.endswith('.jpg'))]
forcurrIndex,filenameinenumerate(img_files):
ifnotos.path.exists(img_files[currIndex]):
print('notexist',img_files[currIndex])
break
img=cv2.imread(img_files[currIndex])
img1=cv2.imread(img_files[currIndex+1])
ssim=compare_ssim(img,img1,multichannel=True)
ifssim>0.9:
imgs_n.append(img_files[currIndex+1])
print(img_files[currIndex],img_files[currIndex+1],ssim)
else:
print('small_ssim',img_files[currIndex],img_files[currIndex+1],ssim)
currIndex+=1
ifcurrIndex>=len(img_files)-1:
break
forimageinimgs_n:
#yidong(image,save_path_img)
delete(image)

方法二:逐一比较,如果相似,则从原始列表中删除,添加到新列表中,如果不相似,则继续

例如,如果有文件1-10,首先比较1和2。如果相似,删除原列表并添加到新列表中,然后比较1和3。如果不相似,则继续比较1和4...一直比较,直到最后一个,然后继续,通常应该从2开始比较,但2被删除,所以从3开始,继续以前的操作,最后删除新列表。

代码如下:

#!/usr/bin/envpython
#-*-coding:utf-8-*-
#@Time:2019/1/1612:03
#@Author:xiaodai
importos
importcv2
fromskimage.measureimportcompare_ssim
importshutil
importdatetime
defyidong(filename1,filename2):
shutil.move(filename1,filename2)
defdelete(filename1):
os.remove(filename1)
print('real_time:',now_now-now)
if__name__='__main__':
path=r'F:\temp\demo'
#save_path_img=r'F:\temp\demo_save'
#os.makedirs(save_path_img,exist_ok=True)
for(root,dirs,files)inos.walk(path):
fordircindirs:
ifdirc=='rec_pic':
pic_path=os.path.join(root,dirc)
img_path=pic_path
imgs_n=[]
num=[]
del_list=[]
img_files=[os.path.join(rootdir,file)forrootdir,_,filesinos.walk(img_path)forfileinfilesif
(file.endswith('.jpg'))]
forcurrIndex,filenameinenumerate(img_files):
ifnotos.path.exists(img_files[currIndex]):
print('notexist',img_files[currIndex])
break
new_cur=0
foriinrange(10000000):
curindex=new_cur
ifcurindex>=len(img_files)-currIndex-1:
break
else:
size=os.path.getsize(img_files[curindex+currIndex+1])
ifsize<512:
#delete(img_files[currIndex+1])
del_list.append(img_files.pop(curindex+currIndex+1))
else:
img=cv2.imread(img_files[currIndex])
img=cv2.resize(img,(46,46),interpolation=cv2.INTER_CUBIC)
img1=cv2.imread(img_files[currindex1+currIndex+1])
img1=cv2.resize(img1,(46,46),interpolation=cv2.INTER_CUBIC)
ssim=compare_ssim(img,img1,multichannel=True)
ifssim>0.9:
#imgs_n.append(img_files[currIndex+1])
print(img_files[currIndex],img_files[currindex1+currIndex+1],ssim)
del_list.append(img_files.pop(currindex1+currIndex+1))
new_cur=curindex
else:
new_cur=curindex+1
print('small_ssim',img_files[currIndex],img_files[curindex+currIndex+1],ssim)
forimageindel_list:
#yidong(image,save_path_img)
delete(image)
print('delete',image)

相关文章

python中如何删除相似的图片?

python中如何删除相似的图片?

2026-01-29
python中能输出中文吗?

python中能输出中文吗?

2026-01-29
python怎么跳转界面?

python怎么跳转界面?

2026-01-28
python怎么运行js文件?

python怎么运行js文件?

2026-01-28
python怎么计算面积?

python怎么计算面积?

2026-01-28
在mac系统怎么编写Python?

在mac系统怎么编写Python?

2026-01-28