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

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
Help Net Security
Help Net Security
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
C
Cisco Blogs
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
L
LINUX DO - 热门话题
Security Latest
Security Latest
A
Arctic Wolf
G
GRAHAM CLULEY
月光博客
月光博客
S
Securelist
D
Docker
J
Java Code Geeks
T
Troy Hunt's Blog
T
Tenable Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
SecWiki News
SecWiki News
S
Security @ Cisco Blogs
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LINUX DO - 最新话题
Recent Commits to openclaw:main
Recent Commits to openclaw:main
aimingoo的专栏
aimingoo的专栏
博客园 - 【当耐特】
H
Heimdal Security Blog
The Hacker News
The Hacker News
博客园 - 三生石上(FineUI控件)
Application and Cybersecurity Blog
Application and Cybersecurity Blog
N
Netflix TechBlog - Medium
Vercel News
Vercel News
Forbes - Security
Forbes - Security
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
IT之家
IT之家
B
Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
S
Secure Thoughts
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Check Point Blog
云风的 BLOG
云风的 BLOG
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Blog of Author Tim Ferriss
L
Lohrmann on Cybersecurity
F
Full Disclosure
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed

Lan小站-嗯,不错! - Python

从 pip 到 uv:一场 Python 包管理的「换引擎」革命 通过终端管理宝塔Python项目管理器里面的Python项目 - Lan小站-嗯,不错! requests优雅的重试 - Lan小站-嗯,不错! 某牛某客专栏文章爬虫 - Lan小站-嗯,不错! 解决Mac下ssl.SSLCertVerificationError:[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate(_ssl.c:1056) Debian11安装部署stable-diffusion-webui记录 - Lan小站-嗯,不错! 调用ChatGPT超过4096Token后自动截取保留指定长度的Token - Lan小站-嗯,不错! django怎么在迁移数据库的时候,自动生成数据 - Lan小站-嗯,不错! python datetime 东八区时间 - Lan小站-嗯,不错!
滑块验证图片匹配 - Lan小站-嗯,不错!
Lan · 2026-03-03 · via Lan小站-嗯,不错! - Python
    bg_w, bg_h, bg_ch, bg_rows = decode_png(bg_path)
    bk_w, bk_h, bk_ch, bk_rows = decode_png(block_path)
    if bg_ch != 3:
        raise ValueError("background.png 需要是 RGB 图")
    if bk_ch != 4:
        raise ValueError("block.png 需要是 RGBA 图")

    bg_gray = to_gray(bg_rows, bg_ch)
    bk_gray = to_gray(bk_rows, bk_ch)
    bk_mask = alpha_mask(bk_rows, bk_ch)
    tpl_gray, tpl_mask = crop_by_mask(bk_gray, bk_mask)

    bg_edges = edge_map(bg_gray)
    tpl_edges = edge_map(tpl_gray)

    x, y, score = find_best_position(bg_edges, tpl_edges, tpl_mask)

    print(f"背景图尺寸: {bg_w}x{bg_h}")
    print(f"滑块图尺寸: {bk_w}x{bk_h}")
    print(f"缺口左上角坐标: (, {y})")
    print(f"建议滑动距离(像素): ")
    print(f"匹配误差(越小越好): {score:.2f}")


if __name__ == "__main__":
    main()

核心原理

本质是:把滑块图当“模板”,在背景图里滑动对比,找到误差最小的位置。

参考实现都在
main.py

分步骤对应你看到的图片

  1. step_01 灰度化
    把 RGB 转灰度,减少颜色维度干扰,只保留亮度结构。
  2. step_02 Alpha 掩码
    block.png 的透明通道提取有效像素(非透明=1,透明=0),只比较有意义区域。
  3. step_03 背景边缘图
    对背景灰度图做简易梯度(和左/上像素差分),突出轮廓。
  4. step_04 模板边缘图
    对裁剪后的滑块模板做同样边缘处理,保证比较口径一致。
  5. 模板匹配(关键)
    在背景每个候选 (x,y) 上,按掩码点计算 SSE 误差:
    [
    score(x,y)=\frac{1}{N}\sum (bg\_edgey+py-tpl\_edgepy)^2
    ]
    score 最小的位置作为缺口位置。
  6. step_05 / step_06 可视化
    step_05 画匹配框;step_06 把滑块半透明叠加到匹配位置,验证直观效果。