first commit

This commit is contained in:
douboer
2026-02-06 11:56:47 +08:00
commit 3aed987c75
66 changed files with 1393 additions and 0 deletions

102
USAGE.md Normal file
View File

@@ -0,0 +1,102 @@
# 使用示例与说明
## 🚀 快速开始
### 1. 安装依赖
```bash
pip install -r requirements.txt
```
### 2. 安装 potrace必需
```bash
brew install potrace
```
### 2.1 字体转SVG依赖
```bash
pip install fonttools uharfbuzz
```
### 3. 转换单个文件
```bash
python pic2svg.py images/your_image.png
```
### 3.1 指定输出目录
```bash
python pic2svg.py images/your_image.png --outdir output/
```
### 4. 批量转换
```bash
python pic2svg.py --indir images --outdir output
```
### 5. 字体转SVG
```bash
python font2svg.py --font path/to/font.ttf --text "Hello"
python font2svg.py --font path/to/font.ttf --text "你好" --outdir output
python font2svg.py --font path/to/font.ttf --text "Hello" --letter-spacing 20
python font2svg.py --fontdir font --text "星程紫微" --outdir svg
```
说明:单字体输出文件名根据 `--text` 自动生成;使用 `--fontdir` 时会加上字体名作为前缀。
## ⚙️ 参数说明
- `--threshold`固定阈值0-255默认使用 Otsu 自动阈值。
- `--indir`:输入目录(批量转换)。
- `--outdir`:输出目录(自动创建,使用输入文件名.svg
- `--turdsize`:抑制噪点面积阈值,越小保留细节越多。
- `--opttolerance`:曲线优化容差,越大文件越小但可能失真。
- `--unit`:坐标量化单位,`1` 表示不量化。
- `--optimize-curves`:开启曲线优化(更小但可能略失真)。
- `--circle-fit`:圆拟合误差阈值(相对半径),>0 启用圆替代。
- `--font`字体文件路径ttf/otf
- `--fontdir`字体目录遍历ttf/otf
- `--text`:文字内容。
- `--letter-spacing`:字距(字体单位),默认 0。
## 🧩 常用配置示例
```bash
# 保真优先(默认参数)
python pic2svg.py images/your_image.png --turdsize 0 --opttolerance 0 --unit 1
# 文件更小(可能略失真)
python pic2svg.py images/your_image.png --optimize-curves --opttolerance 0.2
# 需要固定阈值时
python pic2svg.py images/your_image.png --threshold 128
# 圆拟合简化(仅当轮廓接近圆时生效)
python pic2svg.py images/your_image.png --circle-fit 0.02
# 批量转换
python pic2svg.py --indir images --outdir output
```
## 🐛 常见问题
**Q: 细节丢失或断裂?**
A: 降低 `--turdsize`,关闭 `--optimize-curves`,必要时指定 `--threshold`
**Q: SVG 太大?**
A: 开启 `--optimize-curves`,或适当增大 `--opttolerance`
**Q: 能否处理彩色图?**
A: 当前流程会转为灰度并二值化,只保留黑色区域。
**Q: 圆拟合过于粗糙?**
A: 减小 `--circle-fit` 或关闭圆拟合。
## 📄 License
MIT License - 自由使用和修改