现实中我们经常需要用到图像去重,比如为了扩充人脸图像,可以在百度、Google通过关键词下载大量人脸图像,但这些图像可能存在重复,在合并时需要去重。
开源地址:
https://github.com/idealo/imagededup
该库于今年4月份开源,已经有1600+颗星,最近两天还冲上了Github趋势榜。
可以使用 pip 直接安装:
- pip install imagededup
仅需要 4 行代码即可实现图像去重:
- from imagededup.methods import PHash
- phasher = PHash()
-
- # 生成图像目录中所有图像的二值hash编码
- encodings = phasher.encode_images(image_dir='path/to/image/directory')
-
- # 对已编码图像寻找重复图像
- duplicates = phasher.find_duplicates(encoding_map=encodings)
-
- # 给定一幅图像,显示与其重复的图像
- from imagededup.utils import plot_duplicates
- plot_duplicates(image_dir='path/to/image/directory',
- duplicate_map=duplicates,
- filename='ukbench00120.jpg')
项目中应用实例
- """
- 图片去重
- """
- import os
- from imagededup.methods import PHash
-
-
- def process_file(img_path):
- """
- 处理图片去重
- :return:
- """
- try:
- phasher = PHash()
- # 生成图像目录中所有图像的二值hash编码
- encodings = phasher.encode_images(image_dir=img_path)
- # print(encodings)
- # 对已编码图像寻找重复图像
- duplicates = phasher.find_duplicates(encoding_map=encodings)
- # print(duplicates)
- only_img = [] # 唯一图片
- like_img = [] # 相似图片
-
- for img, img_list in duplicates.items():
- if ".png" in img:
- continue
- if img not in only_img and img not in like_img:
- only_img.append(img)
- like_img.extend(img_list)
-
- # 删除文件
- for like in like_img:
- like_src = os.path.join(img_path, like)
- png_src = like_src[:-4] + ".png"
- if os.path.exists(like_src):
- os.remove(like_src)
- if os.path.exists(png_src):
- os.remove(png_src)
-
- except Exception as e:
- print(e)
-
-
- if __name__ == "__main__":
- img_path = "/tmp/t3/"
-
- num = 0
- for root, dirs, files in os.walk(img_path):
- for dir in dirs:
- file_dir_path = os.path.join(root, dir)
- process_file(file_dir_path)
- num += 1
- print("处理文件夹个数:{}".format(num))