安装 pillow,pytesseract ,安装该模块之后,还需要安装 tesseract-ocr 。

(PS:如果安装了pip,可以python的scripts文件下,输入cmd,然后输入pip install pillow安装最新版的pillow,如果需要安装其它版本的则要自己下载安装,安装其它第三方库都可用这种方法。)

tesseract-ocr 下载地址: https://digi.bib.uni-mannheim.de/tesseract/

 

本次测试下载的是 tesseract-ocr-setup-4.00.00dev.exe ,这块的过程遇到好几个问题。

FileNotFoundError: [WinError 2] 系统找不到指定的文件。

pytesseract.pytesseract.TesseractError: (2, 'Usage: python pytesseract.py [-l lang] input_file')

pytesseract.pytesseract.TesseractError: (1, 'Error opening data file \\Program Files (x86)\\Tesseract-OCR\\eng.traineddata')

 

这几个问题主要是需要安装配置Tesseract-OCR,

1. 下载安装tesseract-ocr,

2. 添加环境变量: TESSDATA_PREFIX = C:\Program Files (x86)\Tesseract-OCR     (PS:在环境变量中新添加变量:TESSDATA_PREFIX ,值(路径)为:C:\Program Files (x86)\Tesseract-OCR(安装路径))

3. 编辑文件 D:\Python35\Lib\site-packages\pytesseract\pytesseract.py

tesseract_cmd = 'tesseract' 

改为:

tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'

 

 

# -*- coding: utf-8 -*-

from PIL import Image
import pytesseract
from selenium import webdriver
class code():
    def __init__(self,imgelement):
        self.imgelement=imgelement
    def Captcha(self):
        pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract'
        url='https://www.zac-esd.com/account/login'
        driver = webdriver.Chrome()
        river.maximize_window()  #将浏览器最大化
        river.get(url)
        driver.save_screenshot('f://aa.png')  #截取当前网页,该网页有我们需要的验证码
        imgelement = driver.find_element_by_id('imgSendCaptcha')  #定位验证码
        location = self.imgelement.location  #获取验证码x,y轴坐标
        size=self.imgelement.size  #获取验证码的长宽
        rangle=(int(location['x']),int(location['y']),int(location['x']+size['width']),int(location['y']+size['height'])) #写成我们需要截取的位置坐标
        i=Image.open("f://aa.png") #打开截图
        frame4=i.crop(rangle)  #使用Image的crop函数,从截图中再次截取我们需要的区域
        frame4.save('f://frame4.jpg')
        qq=Image.open('f://frame4.jpg')
        text=pytesseract.image_to_string(qq).strip() #使用image_to_string识别验证码
        print text[0:4]
        code=text[0:4]
        return code

 

感觉识别非常不准,识别率还是比较低的

 

posted on 2019-03-23 15:01  李大幽默  阅读(377)  评论(0编辑  收藏  举报