惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
Y
Y Combinator Blog
美团技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 【当耐特】
S
SegmentFault 最新的问题
IT之家
IT之家
Recent Announcements
Recent Announcements
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
阮一峰的网络日志
阮一峰的网络日志
T
The Blog of Author Tim Ferriss
Martin Fowler
Martin Fowler
Microsoft Azure Blog
Microsoft Azure Blog
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
U
Unit 42
WordPress大学
WordPress大学
博客园 - Franky
L
LangChain Blog
人人都是产品经理
人人都是产品经理
小众软件
小众软件
博客园 - 叶小钗
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
大猫的无限游戏
大猫的无限游戏
云风的 BLOG
云风的 BLOG
Vercel News
Vercel News
雷峰网
雷峰网
腾讯CDC
Google DeepMind News
Google DeepMind News
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Help Net Security
Help Net Security
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News and Events Feed by Topic
V2EX - 技术
V2EX - 技术
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Schneier on Security
Schneier on Security
博客园 - 聂微东
A
Arctic Wolf
H
Heimdal Security Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Exploit Database - CXSecurity.com
C
Cyber Attacks, Cyber Crime and Cyber Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Google DeepMind News
Google DeepMind News

杜老师说

雨天出差的 3 件必备 桌面零食收纳的 2 个分区 演示前一晚的 2 个检查清单 培训讲师的 PPT 怎么"听完" 出差住酒店插座的 3 个用法 培训笔记只记关键字的 3 个好处 通勤最后 10 分钟的"减速"技巧 两人吃外卖的 2 个小默契 深夜吃夜宵的 3 个节制技巧 橘猫经常跳上书桌的 3 个应对 居家办公桌前的 4 个微习惯 培训归来:把新命令收纳到 shell 的命名约定 高铁出差路上的电源与网络 3 个 fallback 夏季骑行通勤的 3 个小习惯 推荐酷鸭数据主机 培训归来:把新学到的命令收纳到 shell 的 2 个动作 黄昏漫步的"下班缓冲"为什么重要 酒店 Wi-Fi 不稳的 3 个 fallback 工具 培训归来:怎样把新学到的命令收纳到自己的 shell 高铁出差路上的 4 个阅读习惯 macOS 终端下本地跑 LLM 实测 工作室暗光环境的护眼心得 通勤党耳机选型:3 款降噪豆横评 深夜 Terminal 的 4 个提效小动作 Minecraft 生存服务器来啦「1.21.8 版」 快速部署 Nexus Terminal 打造高颜值远程管理终端 DEB 和 RPM 有什么区别 ima 任务模式邀请码分享 Temp Mail 兼具隐私和便捷性的最佳临时电子邮件服务 Dockerd 日志太多?磁盘爆了?一篇教您搞定容器日志问题 二手手机值得买吗?数码爱好者的省钱换机经验 最近总觉得脑子不够用?杜老师的脑力不足自救实录 一站式操作系统 ISO 下载平台 MinIO 社区版 Web 管理界面被删事件全解析 如何使用 PHP 脚本筛选去不图床已到期用户邮箱 Focalboard 开源项目管理的有力工具 NodePass 开源的隧道工具重新定义内网穿透 探索网络实验的神器 Mininet Ubuntu 系统无法通过 pip 命令安装 Python 库问题解决 几种通过 FFmpeg 无损压缩视频的方法 天津游记「多图预警」 合肥游记「多图预警」
使用 Python 脚本实现图片相似度匹配
Teacher Du · 2025-09-16 · via 杜老师说

随着相机像素越来越大,图片体积也变大了。在图片处理中,较大的文件体积会影响性能,因此杜老师会先生成缩略图,筛选完成后再通过 Python 脚本实现图片相似度匹配。这里是一个简单的示例,供需要的小伙伴们参考。

脚本说明

以下是个基于 Python 的脚本,使用 PIL 以及 imagehash 库来实现。

遍历目录 A 中所有图片。

在目录 B 中查找相似的图片「通过感知哈希算法判断」

如找到匹配项,则将图片复制到目录 C,并以目录 A 图片的名字命名。

安装依赖

1
pip install pillow imagehash

注意:在运行脚本前,需安装所需的 Python 库。

脚本示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
import shutil
from PIL import Image
import imagehash

# 定义目录路径
dir_a = 'path/to/dirA'
dir_b = 'path/to/dirB'
dir_c = 'path/to/dirC'

# 设置相似度阈值(越小越严格)
threshold = 5

# 获取图片的感知哈希值
def get_image_hash(filepath):
try:
return imagehash.phash(Image.open(filepath))
except Exception as e:
print(f"无法处理文件 {filepath}: {e}")
return None

# 判断两个哈希值是否相似
def is_similar(hash1, hash2):
return hash1 - hash2 <= threshold

# 确保目标目录存在
os.makedirs(dir_c, exist_ok=True)

# 遍历目录 A
for filename in os.listdir(dir_a):
file_a_path = os.path.join(dir_a, filename)

# 检查是否为图片
if not filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
continue

hash_a = get_image_hash(file_a_path)
if hash_a is None:
continue

# 遍历目录 B 寻找相似图片
for b_filename in os.listdir(dir_b):
file_b_path = os.path.join(dir_b, b_filename)

# 检查是否为图片
if not b_filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
continue

hash_b = get_image_hash(file_b_path)
if hash_b is None:
continue

if is_similar(hash_a, hash_b):
# 构建目标路径
file_c_path = os.path.join(dir_c, filename)
# 复制并重命名文件
shutil.copy(file_b_path, file_c_path)
print(f"已找到匹配: {filename} -> {b_filename}, 已复制到 {file_c_path}")

注意:将 dir_a, dir_bdir_c 替换为实际路径;threshold 控制图像相似度阈值,可以根据需要调整;支持多种常见格式图片文件;使用 imagehash.phash 进行感知哈希的比较,适合用于识别视觉上接近的图片。

运行效果

1
2
3
4
5
(myenv) penn@penn-VMware-Virtual-Platform:~/图片$ python3 1.py
已找到匹配: image105.jpg -> 1745928332994.jpg, 已复制到 c/image105.jpg
已找到匹配: image001.jpg -> 1745736425856.jpg, 已复制到 c/image001.jpg
已找到匹配: image017.jpg -> 1745736425221.jpg, 已复制到 c/image017.jpg
已找到匹配: image085.jpg -> 1745928334851.jpg, 已复制到 c/image085.jpg

注意:脚本运行过程可能会有错误提示,需要根据提示进行修复。