0x00 下载安装tesseract

1、下载地址

http://digi.bib.uni-mannheim.de/tesseract/

2、安装成功后,配置环境变量

网络爬虫 -- 验证码识别_爬虫

3、检查是否设置成功

tesseract -v

网络爬虫 -- 验证码识别_python_02

4、安装tesseract库和pillow库文件

pip3 install tesserocr pillow

0x01 识别测试

1、将该图片保存到桌面

网络爬虫 -- 验证码识别_numpy_03

2、代码实现,识别有误差

import pytesseract
from PIL import Image


image=Image.open('123.png')
res=pytesseract.image_to_string(image)
print(res)


运行结果: 65ab

0x02 处理图片再识别

有时候识别的时候有问题,我们可以修改一些识别值,将图片转成黑白色,通过线面代码修改hd这个值,会提高一定的识别率

import pytesseract
from PIL import Image
import numpy as np


image=Image.open('123.png')
image=image.convert('L')
hd=150
sz=np.array(image)
sz=np.where(sz > hd,255,0)
image=Image.fromarray(sz.astype('uint8'))
#image.show()
res=pytesseract.image_to_string(image)


print(res)

0x03 声明

《Python3网络爬虫开发实战 第二版》章节内容改编。

仅供安全研究与学习之用,若将工具做其他用途,由使用者承担全部法律及连带责任,作者不承担任何法律及连带责任。

欢迎关注公众号编程者吧

网络爬虫 -- 验证码识别_Image_04