Python3使用 pytesseract 进行图片识别

一、安装Tesseract-OCR软件

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

二、Python中使用

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

1. 安装依赖

1 pip install pytesseract
2 pip install pillow

2. 编写代码

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

代码如下:

1 import pytesseract
2 from PIL import Image
3 
4 image = Image.open("code.png")
5 code = pytesseract.image_to_string(image)
6 print(code)

结果为6067,识别成功。

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

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

1 import pytesseract
2 from PIL import Image
3 
4 pytesseract.pytesseract.tesseract_cmd = 'D:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
5 tessdata_dir_config = '--tessdata-dir "D:/Program Files (x86)/Tesseract-OCR/tessdata"'
6 
7 image = Image.open("code.png")
8 code = pytesseract.image_to_string(image, config=tessdata_dir_config)
9 print(code)

 

--------------------------------------------------------------------------------------------------------------------

talk is cheap , show me the code.

 

猜你喜欢

转载自www.cnblogs.com/chenshengkai/p/11318387.html