pytesseract识别验证码教程

2017-09-15  本文已影响2067人  慢慢慢慢热

1、简介

​ a、Python-tesseract 是一个基于 google's Tesseract-OCR 的独立封装包;

​ b、Python-tesseract 功能是识别图片文件中文字,并作为返回参数返回识别结果;

​ c、Python-tesseract 默认支持 tiff、bmp 格式图片,只有在安装 PIL 之后,才能支持 jpeg、gif、png 等其他图片格式;

​ D、Python-tesseract的使用需要安装tesseract-ocr安装包(windows安装文件)

2、下载及安装

2.1下载tesseract-ocr文件

2.1.1 官方发布的 3.02 版本下载地址

2.1.2 德国曼海姆大学发行的 3.05 版本下载地址

2.1.3 另一个版本

我是用的是第二个版本

2.2安装tesseract-ocr文件

直接将下载文件双击安装,默认在 C:\Program Files (x86)\Tesseract-OCR

安装完成后,将 C:\Program Files (x86)\Tesseract-OCR添加至环境变量Path下面,这里是为了之后Python-tesseract在识别时可以调用tesseract.exe

并新建环境变量 TESSDATA_PREFIX,将C:\Program Files (x86)\Tesseract-OCR\tessdata添加进去,这里是在识别时能打开训练数据,不然可能报错 pytesseract.pytesseract.TesseractError: (1,'Error opening data file \Program Files (x86)\Tesseract-OCR\tessdata/eng.traineddata')

完成上述操作后,打开cmd,输入tesseract,能打印帮助信息即成功

2.3下载PIL、pytesseract

打开cmd或shell,执行

pip install PIL

pip install pytesseract

等待下载安装完成,打开pycharm输入

import pytesseract

import PIL

没有报错就ok

3、使用

3.1获取验证码位置及大小

location = driver.find_element_by_xpath(xpath_str).location

print(location) # 表示当前元素的位置,x:横坐标,y:纵坐标

{'y': 411, 'x': 849}

size= driver.find_element_by_xpath(xpath_str).size

print(size) # 表示当前元素的宽高,width:宽,height:高

{'width': 48, 'height': 20}

left = location['x']
top =  location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
box = (left, top, right, bottom)

box就表示当前验证码的坐标,这样就可以进行裁剪了

完整代码

#!/usr/bin/env python
# coding=utf-8

# Created by slowchen on 2017/9/15 15:34.

from selenium import webdriver
import pytesseract
import time
from PIL import Image, ImageEnhance

PostUrl = 'http://yjsymis.hrbeu.edu.cn/gsmis/indexAction.do'
driver = webdriver.Chrome()
driver.get(PostUrl)
driver.maximize_window()
driver.get_screenshot_as_file(r'E:\\image.jpg')
# 获取图片位置
location = driver.find_element_by_xpath("/html/body/form/table/tbody/tr[3]/td/table/tbody/tr[2]/td/table/tbody/tr[2]/td/table[1]/tbody/tr[3]/td[1]/img").location
# 获取图片大小
size = driver.find_element_by_xpath("/html/body/form/table/tbody/tr[3]/td/table/tbody/tr[2]/td/table/tbody/tr[2]/td/table[1]/tbody/tr[3]/td[1]/img").size

left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
im = Image.open(r'E:\\image.jpg')
box = (left, top, right, bottom)
region = im.crop(box)
region.save(r"e:/image_code.jpg")
time.sleep(1)
driver.quit()
ima = Image.open(r"E:\\image_code.jpg")
imgry = ima.convert('L')  # 图像加强,二值化
sharpness = ImageEnhance.Contrast(imgry)  # 对比度增强
sharp_img = sharpness.enhance(2.0)
sharp_img.save(r"E:\\image_code.jpg")
code = pytesseract.image_to_string(Image.open(r"E:\\image_code.jpg"))  # code即为识别出的图片数字str类型
print(code)

注意:倒数第二行code = pytesseract.image_to_string(Image.open(r"E:\\image_code.jpg"))

image_to_string()括号内的内容不能直接用文件路径,必须调用Image.open()方法打开,否则报错

AttributeError: 'str' object has no attribute 'save'

4、参考

1、https://segmentfault.com/q/1010000007964197

2、http://www.cnblogs.com/zhongtang/p/5560361.html

3、http://www.cnblogs.com/zhongtang/p/5554784.html

4、http://blog.csdn.net/qianbinbin1122/article/details/53696202

5、http://blog.csdn.net/mrlevo520/article/details/51901579

上一篇下一篇

猜你喜欢

热点阅读