Skip to main content

Text Eraser

[![CI](https://github.com/lehuaner/Text Eraser/actions/workflows/ci.yml/badge.svg)](https://github.com/lehuaner/Text Eraser/actions/workflows/ci.yml) PyPI Python License: MIT

图片文字擦除工具 — DBNet 文字检测 + PatchMatch 内容识别填充,附带去发光(去除绿光/辉光文字的光晕)与本地 Web 界面。 Image text removal via DBNet text detection + PatchMatch-based content-aware fill, with glow removal and a local web UI.

demo

特性

  • 一键擦除文字:DBNet(PP-OCRv4 det ONNX,约 5MB,CPU 推理)定位文字框,框内 Otsu 生成逐像素字形蒙版,PatchMatch 算法用周围背景填充
  • 去发光(deglow):绿光/辉光文字的光晕污染背景,普通填充会残留绿斑——v2 方案用 alpha 分解 + 调和场插值恢复干净背景
  • 所见即所得:「移动边缘」让展示蒙版与真实填充区完全一致,支持自动判定边缘外扩量
  • 纯本地运行:无 API、无上传,模型首次运行自动下载后完全离线
  • 两种用法:本地 Web 界面(拖图即擦)或 Python 库调用(erase_text() 一步出结果)

安装

pip install text-eraser

需要 Python 3.10+。DBNet 模型(约 5MB)在首次使用时自动从 HuggingFace 下载,之后离线可用。

PyPI 发行名为 text-eraser(曾用名 textpatch 与既有包名过于相似未获准),Python 导入名是 text_eraser

从源码运行:

git clone https://github.com/lehuaner/Text Eraser.git
cd Text Eraser
pip install -e .

快速上手

Web 界面

text-eraser            # 或 python -m text_eraser

浏览器打开 http://127.0.0.1:8765/,拖入图片即可擦除;支持逐面板查看蒙版/去发光中间结果、调整参数、保留历史记录。

  • 端口/地址:环境变量 TEXT_ERASER_PORT(默认 8765)、TEXT_ERASER_HOST(默认 127.0.0.1)
  • 运行数据目录:TEXT_ERASER_DATA_DIR(仓库开发用 data/,pip 安装默认 ~/.text_eraser/data
  • 模型缓存目录:TEXT_ERASER_MODEL_DIR(pip 安装默认 ~/.text_eraser/models

Python 库调用

from PIL import Image
from text_eraser import erase_text, to_rgb_uint8

rgb = to_rgb_uint8(Image.open("demo.png"))       # HxWx3 uint8 RGB
result, mask, meta = erase_text(rgb, return_mask=True)
Image.fromarray(result).save("out.png")
# mask: 255=被擦除的文字; meta: mask_pix / inpaint_seconds / edge_used 等

只用填充器(去水印/去任意内容,无需文字检测):

import numpy as np
from text_eraser import inpaint

hole = np.zeros(rgb.shape[:2], np.uint8)   # >0 = 要清除的区域
hole[10:40, 20:80] = 255
filled = inpaint(rgb, hole, sample_mask=255 - hole)

主要参数(erase_text

参数 默认 说明
edge 1 「移动边缘」:蒙版与填充区同步外扩(>0)/收缩(<0)像素
auto_edge True 按文字色残留自动判定最小外扩(多数图 1,硬图自动到 2)
q_off 55 蒙版紧密度 [30,70],越高越贴字形
direction None 纹理方向角度°(木纹/条带类背景)
deglow_scheme "v2" 去发光方案:"v2" / "off"(无发光图自动零改动)
max_side 960 DBNet 推理最长边,调大可提升小字召回

完整函数级 API 见 docs/ALGORITHM.md

算法概览

原图 RGB
  → DBNet 文字框 → 框内 Otsu + 纯白补全 → 逐像素文字蒙版
  → 去发光 v2 (强绿信号门 → alpha 分解恢复背景 → 调和场插值)
  → 移动边缘 → PatchMatch 填充 (Criminisi 优先级 + 颜色自适应 + TELEA 兜底)
  → 擦除结果

已知限制

  • 发光文字路径仍在迭代:绿光/辉光场景经过多轮修复已大幅改善,但个别复杂背景(暖色多弧段等)仍可能有色差残留,持续按实际观感调整中
  • 文字检测依赖 DBNet 召回;极端小字(<8px 高)或严重模糊的字体可能漏检
  • 大图(4K+)CPU 推理约 0.1s/张,填充耗时与蒙版面积成正比

开发

pip install -e .[dev]
pytest                 # 合成图测试套件
Text Eraser/
├── text_eraser/          # 包本体 (检测/蒙版/填充/去发光/Web)
├── tests/              # 合成图测试 (CI 用, 自足不依赖样图)
├── docs/
│   ├── ALGORITHM.md    # 函数级算法与参数指南
│   ├── DEGLOW_V4.md    # 去发光 v4 规格说明
│   ├── assets/         # README 演示图
│   └── dev/            # 开发日志与专项修复报告
├── scripts/            # 离线诊断/回归脚本 (见下)
└── deglow/             # 去发光 v4 实验模块 (不随 pip 包发布)

scripts/ 下还有一套基于真实样图的回归脚本(regress_*.py,样图不入库,需本地自备 data/ 样图),用于算法调参时的逐位回归验证。

发布

发新版本无需本地构建、无需任何 API token——推送 tag 即自动发布到 PyPI(Trusted Publishing / OIDC):

# 1. 升版本号: pyproject.toml 的 version + text_eraser/__init__.py 的 __version__
# 2. 提交后打 tag 推送
git tag v0.1.2 && git push origin main v0.1.2

首次使用前需在 PyPI 一次性登记发布者:项目管理页 → Settings → Publishing, 填 Owner lehuaner / Repository text-eraser / Workflow publish.yml(Environment 留空)。

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

text_eraser-0.1.2.tar.gz (105.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

text_eraser-0.1.2-py3-none-any.whl (103.6 kB view details)

Uploaded Python 3

File details

Details for the file text_eraser-0.1.2.tar.gz.

File metadata

  • Download URL: text_eraser-0.1.2.tar.gz
  • Upload date:
  • Size: 105.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for text_eraser-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5fa54ac963a70cd7d9eabb98297bb381f99576c62b1ccae56d96f7393c7e14eb
MD5 aa2f4b383e0c52e3ff4b761c3e4b870f
BLAKE2b-256 93275c56d59768c07b029a5e3e812c7234940657e029e4ae63d85d6f360575a6

See more details on using hashes here.

Provenance

The following attestation bundles were made for text_eraser-0.1.2.tar.gz:

Publisher: publish.yml on lehuaner/text-eraser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file text_eraser-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: text_eraser-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 103.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for text_eraser-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 61cd8ed72b0a9ec4413a70272aa488840374fb084a7cb95a6c012e25fa3dcba9
MD5 40ad28573bb5ed6498c4026dfc481c9e
BLAKE2b-256 c5b7585beec9dcedf37ea256f334b98e8146f660b180b4ce553515f8827b2dcd

See more details on using hashes here.

Provenance

The following attestation bundles were made for text_eraser-0.1.2-py3-none-any.whl:

Publisher: publish.yml on lehuaner/text-eraser

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.2.1

2 files

0.2.0

2 files

This release

0.1.2 This release

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page