Skip to content

Python

文天祥:人生自古谁无死,留取丹心照汗青

学习尝试

使用imagededup库实现照片去重
py
#!/usr/bin/env python

# FILE Name: Camera.py 
# python Camera.py <img_path>
# pip install imagededup

import sys
import os.path as op

from imagededup.methods import PHash
from imagededup.utils import plot_duplicates
phasher = PHash()


def process_file(img_path):
    print(f'路径:{img_path}')
    # 生成图像目录中所有图像的二值hash编码
    encodings = phasher.encode_images(image_dir=img_path)
    dedup_json = op.join(img_path, 'dedup.json')
    duplicates = phasher.find_duplicates(encoding_map=encodings, outfile=dedup_json)

    # 查找指定图片的重复项并生成快照保存为 dedup_shot.jpg
    # dedup_shot = op.join(img_path, 'dedup_shot.jpg')
    # plot_duplicates(image_dir=img_path, duplicate_map=duplicates, filename='11.png', outfile=dedup_shot)
    only_img = []  # 唯一图片
    like_img = []  # 相似图片

    for img, img_list in duplicates.items():
        if img not in only_img and img not in like_img:
            only_img.append(img)
            like_img.extend(img_list)
    for p in like_img:
        pic_full_path = op.join(img_path, p)
        # if op.exists(pic_full_path):
            # mv(pic_full_path, op.join(img_path, "backup"))
    print('相似图片{}张,唯一图片{}张'.format(len(like_img), len(only_img)))

if __name__ == '__main__':
    if len(sys.argv) > 1:
        process_file(sys.argv[1])
    else:
        print('错误,需要加路径!!!')    # Linux未测试

Copyright © manosP . 2023