要使用 Python 识别图片中的文字,推荐使用 Pillow (PIL) 配合 Tesseract OCR 引擎(通过 pytesseract 库),以下是详细步骤:

步骤 1:安装依赖
-
安装 Tesseract OCR(核心引擎):
- Windows:下载安装包 UB-Mannheim/tesseract
- MacOS:
brew install tesseract - Linux (Debian/Ubuntu):
sudo apt install tesseract-ocr - 语言包(如需要中文):
sudo apt install tesseract-ocr-chi-sim(简体中文)
sudo apt install tesseract-ocr-chi-tra(繁体中文)
-
安装 Python 库:
pip install pillow pytesseract
步骤 2:Python 示例代码
from PIL import Image
import pytesseract
# 设置 Tesseract 路径(Windows 需要指定安装路径)
# pytesseract.pytesseract.tesseract_cmd = r'C:Program FilesTesseract-OCRtesseract.exe'
# 打开图片
image = Image.open('your_image.jpg') # 替换为你的图片路径
# 识别文字(默认英文)
text = pytesseract.image_to_string(image)
# 识别中文(简体)
# text = pytesseract.image_to_string(image, lang='chi_sim')
print("识别结果:")
print(text)
常见问题解决
-
中文识别不准确:

- 确保安装了中文语言包(如
tesseract-ocr-chi-sim)。 - 使用
lang='chi_sim'参数。 - 优化图片质量(清晰、无反光、正对拍摄)。
- 确保安装了中文语言包(如
-
报错
tesseract is not installed:- 检查
tesseract是否在系统路径中。 - Windows 需手动设置路径(取消注释代码中的
tesseract_cmd)。
- 检查
-
提高识别精度:
- 预处理图片:转为灰度、二值化、降噪。
- 调整图片:使用图像处理库(如 OpenCV)增强对比度。
- 指定区域识别:通过
image.crop((x, y, width, height))裁剪局部区域。
预处理增强示例
from PIL import Image, ImageFilter
# 打开图片并预处理
image = Image.open('your_image.jpg')
image = image.convert('L') # 转为灰度
image = image.filter(ImageFilter.SHARPEN) # 锐化
image = image.point(lambda x: 0 if x < 140 else 255) # 二值化
# 识别文字
text = pytesseract.image_to_string(image, lang='chi_sim')
print(text)
替代方案:第三方 API
如果本地识别效果不佳,可使用在线 OCR API:

- 百度 OCR:高精度中文识别(有免费额度)
- Google Vision:英文识别效果好
- 腾讯 OCR:支持多语言
提示:在线 API 需处理网络请求和隐私问题。
图片来源于AI模型,如侵权请联系管理员。作者:酷小编,如若转载,请注明出处:https://www.kufanyun.com/ask/294241.html


评论列表(5条)
这篇教程太及时了!跟着步骤试了下用Pillow+Tesseract识别发票文字,省去了手动输入的麻烦。不过中文识别偶尔会串行,感觉加个图像预处理会更准,期待作者后续优化技巧~
@smart220:对啊,跟着教程试了确实省时!中文识别串行我也遇到过,感觉加个图像预处理像去噪或调亮度会好很多,我自己试过裁剪多余部分也有帮助。期待作者的新技巧~
这篇教程太实用了!我之前一直手动敲图片里的文字,效率低还容易出错。原来用Python+OCR这么简单就能搞定,步骤讲得也清晰,Pillow和Tesseract的组合确实方便。这下处理扫描文档或者截图里的文字就省心多了,收藏起来下次直接抄作业!
这个教程真实用!我之前也用PIL和Tesseract提取过图片文字,组合起来识别效果挺准的,特别适合处理文档扫描。步骤写得简单清晰,新手照着做肯定能上手,不过复杂图片有时需要多调整参数。
这篇文章太实用了!我刚用PIL和pytesseract试了下,提取图片文字又快又准,以前手动打字累死人,现在省了好多时间。教程步骤很清楚,新手也能轻松上手,真心感谢分享!