使用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)
三、效果展示

在这里插入图片描述

四、tesseract识别中文

1、首先从 git 上下载对应的简体中文

https://github.com/tesseract-ocr/langdata/tree/master/chi_sim

2、在 D:\pytesseract\Tesseract-OCR 安装目录下新建任意目录,比如 tessdata,把下载好的数据放到改目录下,且把该目录加入到配环境变量PATH中。
3、测试

#查看所能识别的字体
tesseract --list-langs

#对图片识别
tesseract c.png c -l chi_sim  #就会将 c.png 识别到的图片信息保存到 c.txt 中
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值