码迷,mamicode.com
首页 > 编程语言 > 详细

python + tesseract 识别网站验证码

时间:2016-07-31 06:54:36      阅读:770      评论:0      收藏:0      [点我收藏+]

标签:

首先需要下载tesseract-ocr-setup-3.02.02.exe,来自谷歌的一个ocr识别项目,提供一个地址:

http://pan.baidu.com/s/1jI551Gi

下载后进行安装,本例安装地址是:C:\Program Files (x86)\Tesseract-OCR\tesseract.exe

然后需要在安装python对应的支持库PIL,pytesseract

pip install pil
pip install pytesseract

记录一段测试代码:

 1 # -*- coding: utf-8 -*-
 2 
 3 import urllib.request, urllib.parse
 4 from PIL import Image
 5 from pytesseract import *
 6 
 7 ‘‘‘
 8 获取验证码
 9 ‘‘‘
10 auth_img_url = rhttps://********/authImage?
11 urllib.request.urlretrieve(auth_img_url, auth.jpg)
12 
13 ‘‘‘
14 对图像进行灰度化,二值化处理
15 ‘‘‘
16 img = Image.open(auth.jpg)
17 img_gray = img.convert(L)
18 
19 threshold = 140
20 table = []
21 for i in range(256):
22     if i < threshold:
23         table.append(0)
24     else:
25         table.append(1)
26 
27 out = img_gray.point(table, 1)
28 # out.save(‘auth_b.jpg‘)
29 
30 ‘‘‘
31 识别验证码中的文字
32 ‘‘‘
33 auth = pytesseract.image_to_string(out)
34 
35 print(auth)

ps. 可能出现“FileNotFoundError: [WinError 2] 系统找不到指定的文件”的提示,解决办法是:

打开文件 pytesseract.py,将tesseract_cmd的值修改为全路径

# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
# tesseract_cmd = ‘tesseract‘
tesseract_cmd = rC:\Program Files (x86)\Tesseract-OCR\tesseract.exe

python + tesseract 识别网站验证码

标签:

原文地址:http://www.cnblogs.com/boyryan/p/5722256.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!