使用python如何删除同一文件夹下相似的图片

 更新时间:2021年05月07日 11:03:32   作者:MHyourh  
这篇文章主要给大家介绍了关于利用python如何删除同一文件夹下相似的图片的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言

最近整理图片发现,好多图片都非常相似,于是写如下代码去删除,有两种方法:

注:第一种方法只对于连续图片(例一个视频里截下的图片)准确率也较高,其效率高;第二种方法准确率高,但效率低

方法一:相邻两个文件比较相似度,相似就把第二个加到新列表里,然后进行新列表去重,统一删除。

例如:有文件1-10,首先1和2相比较,若相似,则把2加入到新列表里,再接着2和3相比较,若不相似,则继续进行3和4比较…一直比到最后,然后删除新列表里的图片

代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import cv2
from skimage.measure import compare_ssim
# import shutil
# def yidong(filename1,filename2):
#     shutil.move(filename1,filename2)
def delete(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) for rootdir, _, files in os.walk(path) for file in files if
                 (file.endswith('.jpg'))]
    for currIndex, filename in enumerate(img_files):
        if not os.path.exists(img_files[currIndex]):
            print('not exist', img_files[currIndex])
            break
        img = cv2.imread(img_files[currIndex])
        img1 = cv2.imread(img_files[currIndex + 1])
        ssim = compare_ssim(img, img1, multichannel=True)
        if ssim > 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
        if currIndex >= len(img_files)-1:
            break
    for image in imgs_n:
        # yidong(image, save_path_img)
        delete(image)

方法二:逐个去比较,若相似,则从原来列表删除,添加到新列表里,若不相似,则继续

例如:有文件1-10,首先1和2相比较,若相似,则把2在原列表删除同时加入到新列表里,再接着1和3相比较,若不相似,则继续进行1和4比较…一直比,到最后一个,再继续,正常应该再从2开始比较,但2被删除了,所以从3开始,继续之前的操作,最后把新列表里的删除。

代码如下:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import cv2
from skimage.measure import compare_ssim
import shutil
import datetime
def yidong(filename1,filename2):
    shutil.move(filename1,filename2)
def delete(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) in os.walk(path):
        for dirc in dirs:
            if dirc == '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) for rootdir, _, files in os.walk(img_path) for file in files if
                             (file.endswith('.jpg'))]
                for currIndex, filename in enumerate(img_files):
                    if not os.path.exists(img_files[currIndex]):
                        print('not exist', img_files[currIndex])
                        break
                    new_cur = 0
                    for i in range(10000000):
                        currIndex1 =new_cur
                        if currIndex1 >= len(img_files) - currIndex - 1:
                            break
                        else:
                            size = os.path.getsize(img_files[currIndex1 + currIndex + 1])
                            if size < 512:
                                # delete(img_files[currIndex + 1])
                                del_list.append(img_files.pop(currIndex1 + 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)
                                if ssim > 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 = currIndex1
                                else:
                                    new_cur = currIndex1 + 1
                                    print('small_ssim',img_files[currIndex], img_files[currIndex1 + currIndex + 1], ssim)
                for image in del_list:
                    # yidong(image, save_path_img)
                    delete(image)
                    print('delete',image)

总结

到此这篇关于使用python如何删除同一文件夹下相似图片的文章就介绍到这了,更多相关python删除文件夹相似图片内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用Python实现Office文档(Word/Excel/PowerPoint)批量转换为PDF

    使用Python实现Office文档(Word/Excel/PowerPoint)批量转换为PDF

    在处理不同格式的Office文档(如Word、Excel和PowerPoint)时,将其转换为PDF格式是常见的需求,本文就跟随小编来看看如何使用Python将Word/Excel/PowerPoint批量转换为PDF吧
    2024-10-10
  • Django如何实现RBAC权限管理

    Django如何实现RBAC权限管理

    这篇文章主要介绍了Django如何实现RBAC权限管理问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • python基于BeautifulSoup实现抓取网页指定内容的方法

    python基于BeautifulSoup实现抓取网页指定内容的方法

    这篇文章主要介绍了python基于BeautifulSoup实现抓取网页指定内容的方法,涉及Python使用BeautifulSoup模块解析html网页的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • 使用 Python 的 pprint库格式化和输出列表和字典的方法

    使用 Python 的 pprint库格式化和输出列表和字典的方法

    pprint是"pretty-print"的缩写,使用 Python 的标准库 pprint 模块,以干净的格式输出和显示列表和字典等对象,这篇文章主要介绍了如何使用 Python 的 pprint库格式化和输出列表和字典,需要的朋友可以参考下
    2023-05-05
  • 关于pytorch求导总结(torch.autograd)

    关于pytorch求导总结(torch.autograd)

    这篇文章主要介绍了关于pytorch求导总结(torch.autograd),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-06-06
  • python实现查询IP地址所在地

    python实现查询IP地址所在地

    本文给大家分享的是使用Python实现根据ip138的API查询IP的地理位置的代码,非常的实用,推荐给大家,有需要的小伙伴可以参考下。
    2015-03-03
  • TensorFlow索引与切片的实现方法

    TensorFlow索引与切片的实现方法

    这篇文章主要介绍了TensorFlow索引与切片的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • 解析Python中的生成器及其与迭代器的差异

    解析Python中的生成器及其与迭代器的差异

    生成器是一种特殊的迭代器,而反过来说则就不对了,迭代器在Python中是一个更抽象的概念,以下我们就来详细解析Python中的生成器及其与迭代器的差异
    2016-06-06
  • TensorFlow 显存使用机制详解

    TensorFlow 显存使用机制详解

    今天小编就为大家分享一篇TensorFlow 显存使用机制详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • python使用Geany编辑器配置方法

    python使用Geany编辑器配置方法

    这篇文章主要介绍了python使用Geany编辑器配置方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02

最新评论