python 图像去重 删除重复 相似的图像

#!/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)

参考链接:win_size exceeds image extent. Either ensure that your images are at least 7x7; or pass win_size ex_valueerror: win_size exceeds image extent. either -CSDN博客

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值