使用pytesseract识别图片验证码

在python爬虫时,可能会遇到需要抓取验证码,识别图片中的文字等,这个时候需要对图片验证码进行识别。

一、安装模块

pip install pytesseract
pip install pillow
yum -y install tesseract-ocr

备注:windows中tesseract-ocr 下载地址请点击 ,安装步骤就是点击下一步下一步

二、对验证码识别

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import pytesseract
from PIL import Image
image = Image.open('code.png')

#设置 tesseract 安装路径
pytesseract.pytesseract.tesseract_cmd = r"D:\pytesseract\Tesseract-OCR\tesseract.exe"   #这个是python3的写法
code = pytesseract.image_to_string(image)
print(code)

三、效果展示

使用pytesseract识别图片验证码