pytesseract参数设置
时间: 2025-04-21 12:42:19 浏览: 37
### 如何配置 Pytesseract 参数
Pytesseract 是 Tesseract OCR 的 Python 封装库,允许通过简单的接口调用 Tesseract 功能。要调整 Pytesseract 的参数以优化 OCR 结果,可以按照如下方法操作:
#### 设置语言选项
可以通过 `lang` 参数指定识别的语言。虽然 Azure 计算机视觉 OCR API 支持 25 种语言[^1],但是 Pytesseract 使用的是 Tesseract 自带的支持语言集合。
```python
import pytesseract
text = pytesseract.image_to_string(image, lang='chi_sim') # 中文简体
```
#### 配置页面分割模式 (PSM)
Tesseract 提供了不同的页面分割模式来适应不同类型的输入图片。这些模式可以从单个字符到整页文本不等。默认情况下,会自动检测最佳布局。
```python
custom_config = r'--psm 6'
text = pytesseract.image_to_string(image, config=custom_config)
```
| PSM Mode | Description |
| --- | --- |
| 0 | Orientation and script detection (OSD) only. |
| 1 | Automatic page segmentation with OSD. |
| ... | 更多模式... |
| 13 | Raw line. Treat the image as a single text line, bypassing hacks that are Tesseract-specific. |
#### 定制 OEM(光学引擎模式)
OEM 模式决定了 Tesseract 应该使用的内部处理方式,默认值为 3 表示 LSTM-only 模型。
```python
custom_config = r'-c tessedit_char_whitelist=0123456789 --oem 1'
text = pytesseract.image_to_string(image, config=custom_config)
```
- **0**: Legacy engine only.
- **1**: Neural nets LSTM engine only.
- **2**: Legacy + LSTM engines.
- **3**: Default, based on what is available.
#### 白名单和黑名单字符过滤
有时可能只关心某些特定字符,在这种情况下可以定义白名单或黑名单来进行筛选。
```python
custom_config = r'-c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZ'
text = pytesseract.image_to_string(image, config=custom_config)
```
以上就是一些常用的 Pytesseract 参数配置技巧,可以根据具体应用场景灵活运用上述设置提高 OCR 准确度。
阅读全文
相关推荐


















