from PIL import Image
import pytesseract
# pytesseract.pytesseract.tesseract_cmd = r'<完整路径>\Tesseract-OCR\tesseract.exe' # 如果Tesseract-OCR未添加到系统环境变量中,请指定路径
# 打开图像文件
image = Image.open('example.png')
# 使用pytesseract将图像转换为文本
text = pytesseract.image_to_string(image)
# 打印识别出的文本
print(text)
导入库:
PIL.Image:用于处理图像文件。pytesseract:用于调用Tesseract OCR引擎进行光学字符识别。设置Tesseract路径(可选):
<完整路径> 替换为 Tesseract-OCR 的实际安装路径。打开图像文件:
Image.open() 方法打开图像文件,这里假设图像文件名为 example.png。图像转文本:
pytesseract.image_to_string() 方法将图像中的文字提取出来,并存储在变量 text 中。打印结果:
print() 函数输出识别出的文本。希望这个示例对你有帮助!
上一篇:python manim
下一篇:python venv 指定版本
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站