baoshiwei
2025-04-15 776a127b8db01cd4338f4db2a84ea567a65bff9f
quchong.py
@@ -52,12 +52,9 @@
                result = "两张图相同"
    return result
if __name__ == '__main__':
    load_path = 'E:\yaocai\yinyanghuo'  # 要去重的文件夹
    save_path = 'E:\yaocai\\2\\yinyanghuo'  # 空文件夹,用于存储检测到的重复的照片
def quchongmethod(load_path, save_path):
    os.makedirs(save_path, exist_ok=True)
    # 获取图片列表 file_map,字典{文件路径filename : 文件大小image_size}
    file_map = {}
    image_size = 0
@@ -70,13 +67,11 @@
            # print('the full name of the file is %s' % os.path.join(parent, filename))
            image_size = os.path.getsize(os.path.join(parent, filename))
            file_map.setdefault(os.path.join(parent, filename), image_size)
    # 获取的图片列表按 文件大小image_size 排序
    file_map = sorted(file_map.items(), key=lambda d: d[1], reverse=False)
    file_list = []
    for filename, image_size in file_map:
        file_list.append(filename)
    # 取出重复的图片
    file_repeat = []
    for currIndex, filename in enumerate(file_list):
@@ -93,16 +88,22 @@
        except Exception as e:
            pass
        if(result == "两张图相同"):
        if (result == "两张图相同"):
            file_repeat.append(file_list[currIndex + 1])
            print("相同的图片:", file_list[currIndex], file_list[currIndex + 1])
        else:
            print('不同的图片:', file_list[currIndex], file_list[currIndex + 1])
        currIndex += 1
        if currIndex >= (len(file_list)-2):
        if currIndex >= (len(file_list) - 2):
            break
    # 将重复的图片移动到新的文件夹,实现对原文件夹降重
    for image in file_repeat:
        shutil.move(image, save_path)
        print("正在移除重复照片:", image)
        print("正在移除重复照片:", image)
if __name__ == '__main__':
    load_path = 'E:\yaocai\\chenpi'  # 要去重的文件夹
    save_path = 'E:\yaocai\\2\\chenpi'  # 空文件夹,用于存储检测到的重复的照片
    quchongmethod(load_path,save_path)