Python3使用 pytesseract 进行图片识别

发布时间:2019-06-11 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Python3使用 pytesseract 进行图片识别脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

一、安装Tesseract-OCR软件

参考我的前一篇文章Windows安装Tesseract-OCR 4.00并配置环境变量

二、Python中使用

需要使用 pytesseract 库,官方使用说明请看:https://pypi.python.org/pypi/...

1. 安装依赖


pip install pytesseract
pip install pillow

2. 编写代码

准备识别下面这个验证码:

Python3使用 pytesseract 进行图片识别

代码如下

import pytesseract
From PIL import Image

image = Image.oPEn("code.png")
code = pytesseract.image_to_string(image)
PRint(code)

Python3使用 pytesseract 进行图片识别


结果为6067,识别成功。

3. 如果出现错误,一般是系统变量设置的问题:

解决办法一:根据安装Tesseract软件的步骤配置环境变量,设置好即可。
解决方法二:在代码中添加相关变量参数:

import pytesseract
from PIL import Image

pytesseract.pytesseract.tesseract_cmd = 'D:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
teSSData_dir_config = '--tessdata-dir "D:/Program Files (x86)/Tesseract-OCR/tessdata"'

image = Image.open("code.png")
code = pytesseract.image_to_string(image, config=tessdata_dir_config)
print(code)

Python3使用 pytesseract 进行图片识别

脚本宝典总结

以上是脚本宝典为你收集整理的Python3使用 pytesseract 进行图片识别全部内容,希望文章能够帮你解决Python3使用 pytesseract 进行图片识别所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。