Python pytesseract库使用指南:安装与OCR识别实战
本文详细介绍Python中pytesseract库的安装配置与使用方法。重点说明需本地安装Tesseract-ocr.exe并添加中文语言包,通过pip安装依赖库。提供完整代码示例,展示如何调用image_to_string接口识别图片文字,包括中英文混合识别及预处理参数设置,帮助开发者快速实现OCR功能。
说明
使用pytesseract库的前提是本地计算机必须已安装Tesseract-ocr.exe程序,二者需配合使用。
安装Tesseract时务必勾选中文语言包,因为默认配置仅支持英文字符识别。
安装命令
pip install pytesseract
实例
import pytesseract
from PIL import Image
text = pytesseract.image_to_string(Image.open(r"d:\Desktop\39DEE621-40EA-4ad1-90CC-79EB51D39347.png"))
print(text)
识别结果输出
Using Tesseract OCR with Python
# import the necessary packages
from PIL import Image
import pytesseract
import ergperse
import cv2
import os
# construct the argument parse and parse the arguments
ap = argparse.ArgunentParser()
ap.add_argument("-i", "--image", required-True,
help="path to input image to be OCR'd")
ap.add_argument("-p", "--preprocess", typesstr, default="thresh",
helpe"type of preprocessing to be done")
args = vars (ap.parse_args())
实例扩展:
Python使用pytesseract库识别图片文字
准备
首先需在本地安装Tesseract客户端软件,随后通过包管理器安装pytesseract库。
pip install pytesseract
若环境中未安装PIL(Python Imaging Library),需一并安装,因为后续调用中第一个参数需由Image类返回对象。
pip install PILLOW
实例
import pytesseract
from PIL import Image
img = Image.open("D://aa.jpg")
text = pytesseract.image_to_string(img, lang="chi_sim")
print(text)
到此这篇关于python pytesseract库的实例用法的文章就介绍到这了,更多相关python pytesseract库是什么内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









