from PIL import Image
import pytesseract
# 打开图片文件
image = Image.open('example.png')
# 使用 pytesseract 进行 OCR 识别,提取文字
text = pytesseract.image_to_string(image, lang='chi_sim') # chi_sim 表示使用简体中文语言包
# 输出提取的文字
print(text)
导入库:
PIL.Image:用于处理图像。pytesseract:用于执行光学字符识别(OCR),将图像中的文字转换为文本。打开图片文件:
Image.open() 方法打开图片文件,这里假设图片文件名为 example.png。进行 OCR 识别:
pytesseract.image_to_string() 方法对图片进行 OCR 处理。lang='chi_sim' 参数指定使用简体中文语言包进行识别。输出提取的文字:
text 中,并通过 print() 函数输出。请确保你已经安装了 pytesseract 和 Pillow 库,可以通过以下命令安装:
pip install pytesseract pillow
此外,还需要安装 Tesseract-OCR 引擎,可以在 Tesseract GitHub 页面 获取安装指南。
上一篇:conda升级python版本
下一篇:python 读取json
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站