#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import cv2
from skimage.metrics import structural_similarity
def get_filelist(filepath, filelist):
if os.path.isfile(filepath):
if filepath.endswith('.jpg'):
filelist.append(filepath)
elif os.path.isdir(filepath):
for path in os.listdir(filepath):
new_path = os.path.join(filepath, path)
get_filelist(new_path, filelist)
return filelist
def delete_similar_img(filelist, index=0):
if index == len(filelist):
return
else:
file0 = filelist[index]
if os.path.exists(file0):
img0 = cv2.imread(file0)
for idx in range(index + 1, len(filelist)):
file1 = filelist[idx]
if not os.path.exists(file1):
continue
img1 = cv2.imread(file1)
if img0.size == img1.size:
print(f'{filelist[index]} 与 {filelist[idx]} 尺寸一致')
ssim = structural_similarity(img0, img1, multichannel=True)
if ssim > 0.9:
os.remove(file1)
else:
print(f'{filelist[index]} 与 {filelist[idx]} 尺寸不一致')
index += 1
delete_similar_img(filelist, index)
if __name__ == '__main__':
testPath = r'E:\test1'
fileList = []
imgList = get_filelist(testPath, fileList)
delete_similar_img(imgList)
如果出现如下报错
ValueError: win_size exceeds image extent. Either ensure that your images are at least 7x7; or pass win_size explicitly in the function call, with an odd value less than or equal to the smaller side of your images. If your images are multichannel (with color channels), set channel_axis to the axis number corresponding to the channels.
可将代码修改为
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import cv2
from skimage.metrics import structural_similarity
def get_filelist(filepath, filelist):
if os.path.isfile(filepath):
if filepath.endswith('.jpg'):
filelist.append(filepath)
elif os.path.isdir(filepath):
for path in os.listdir(filepath):
new_path = os.path.join(filepath, path)
get_filelist(new_path, filelist)
return filelist
def delete_similar_img(filelist, index=0):
if index == len(filelist):
return
else:
file0 = filelist[index]
if os.path.exists(file0):
img0 = cv2.imread(file0)
for idx in range(index + 1, len(filelist)):
file1 = filelist[idx]
if not os.path.exists(file1):
continue
img1 = cv2.imread(file1)
if img0.size == img1.size:
print(f'{filelist[index]} 与 {filelist[idx]} 尺寸一致')
# ssim = structural_similarity(img0, img1, multichannel=True)
ssim = structural_similarity(img0, img1, channel_axis=2)
if ssim > 0.9:
os.remove(file1)
else:
print(f'{filelist[index]} 与 {filelist[idx]} 尺寸不一致')
index += 1
delete_similar_img(filelist, index)
if __name__ == '__main__':
testPath = r'D:\Work\TestDir'
fileList = []
imgList = get_filelist(testPath, fileList)
print(imgList)
delete_similar_img(imgList)