Python3使用 pytesseract 进行图片识别

1、安装Tesseract-OCR软件

参考个人前一篇文章:Windows安装Tesseract-OCR 4.00并配置环境变量python

2、Python中使用

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

1. 安装依赖

pip install pytesseract
pip install pillow

2. 编写代码

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

代码以下code

import pytesseract
from PIL import Image

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

clipboard.png
结果为6067,识别成功。ip

3. 若是出现错误,通常是系统变量设置的问题:

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

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)

clipboard.png

相关文章
相关标签/搜索