python下调用pytesseract识别某网站验证码

一、pytesseract介绍

1、pytesseract说明

pytesseract最新版本0.1.6,网址:https://pypi.python.org/pypi/pytesseract

Python-tesseract is a wrapper for google‘s Tesseract-OCR
( http://code.google.com/p/tesseract-ocr/ ). It is also useful as a
stand-alone invocation script to tesseract, as it can read all image types
supported by the Python Imaging Library, including jpeg, png, gif, bmp, tiff,
and others, whereas tesseract-ocr by default only supports tiff and bmp.
Additionally, if used as a script, Python-tesseract will print the recognized
text in stead of writing it to a file. Support for confidence estimates and
bounding box data is planned for future releases.

翻译一下大意:

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

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

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

2、pytesseract安装

INSTALLATION:

Prerequisites:
* Python-tesseract requires python 2.5 or later or python 3.
* You will need the Python Imaging Library (PIL). Under Debian/Ubuntu, this is
the package "python-imaging" or "python3-imaging" for python3.
* Install google tesseract-ocr from http://code.google.com/p/tesseract-ocr/ .
You must be able to invoke the tesseract command as "tesseract". If this
isn‘t the case, for example because tesseract isn‘t in your PATH, you will
have to change the "tesseract_cmd" variable at the top of ‘tesseract.py‘.
Under Debian/Ubuntu you can use the package "tesseract-ocr".

Installing via pip: 
See the [pytesseract package page](https://pypi.python.org/pypi/pytesseract) 
```
$> sudo pip install pytesseract

翻译一下:

a、Python-tesseract支持python2.5及更高版本;

b、Python-tesseract需要安装PIL(Python Imaging Library) ,来支持更多的图片格式;

c、Python-tesseract需要安装tesseract-ocr安装包,具体参看上一篇博文

综上,Pytesseract原理:

1、上一篇博文中提到,执行命令行 tesseract.exe 1.png output -l eng ,可以识别1.png中文字,并把识别结果输出到output.txt中;

2、Pytesseract对上述过程进行了二次封装,自动调用tesseract.exe,并读取output.txt文件的内容,作为函数的返回值进行返回。

二、pytesseract使用

USAGE:
```
> try:
> import Image
> except ImportError:
> from PIL import Image
> import pytesseract
> print(pytesseract.image_to_string(Image.open(‘test.png‘)))
> print(pytesseract.image_to_string(Image.open(‘test-european.jpg‘), lang=‘fra‘))

可以看到:

1、核心代码就是image_to_string函数,该函数还支持-l eng 参数,支持-psm 参数。

用法:
image_to_string(Image.open(‘test.png‘),lang="eng" config="-psm 7")

2、pytesseract里调用了image,所以才需要PIL,其实tesseract.exe本身是支持jpeg、png等图片格式的。

实例代码,识别某公共网站的验证码(大家千万别干坏事啊):

#-*-coding=utf-8-*-
__author__=‘zhongtang‘

import urllib
import urllib2
import cookielib
import math
import random
import time
import os
import htmltool
from pytesseract import *
from PIL import Image
from PIL import ImageEnhance
import re

class orclnypcg:
    def __init__(self):
        self.baseUrl=‘http://jbywcg.lnypcg.com.cn‘
        self.ht=htmltool.htmltool()
        self.curPath=self.ht.getPyFileDir()
        self.authCode=‘‘

    def initUrllib2(self):
        try:
            cookie = cookielib.CookieJar()
            cookieHandLer = urllib2.HTTPCookieProcessor(cookie)
            httpHandLer=urllib2.HTTPHandler(debuglevel=0)
            httpsHandLer=urllib2.HTTPSHandler(debuglevel=0)
        except:
            raise
        else:
             opener = urllib2.build_opener(cookieHandLer,httpHandLer,httpsHandLer)
             opener.addheaders = [(‘User-Agent‘,‘Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11‘)]
             urllib2.install_opener(opener)

    def urllib2Navigate(self,url,data={}):           #定义连接函数,有超时重连功能
        tryTimes = 0
        while True:
            if (tryTimes>20):
                print u"多次尝试仍无法链接网络,程序终止"
                break
            try:
                if (data=={}):
                    req = urllib2.Request(url)
                else:
                    req = urllib2.Request(url,urllib.urlencode(data))
                response =urllib2.urlopen(req)
                bodydata = response.read()
                headerdata = response.info()
                if headerdata.get(‘Content-Encoding‘)==‘gzip‘:
                    rdata = StringIO.StringIO(bodydata)
                    gz = gzip.GzipFile(fileobj=rdata)
                    bodydata = gz.read()
                    gz.close()
                tryTimes = tryTimes +1
            except urllib2.HTTPError, e:
              print ‘HTTPError[%s]\n‘ %e.code
            except urllib2.URLError, e:
              print ‘URLError[%s]\n‘ %e.reason
            except socket.error:
                print u"连接失败,尝试重新连接"
            else:
                break
        return bodydata,headerdata

    def randomCodeOcr(self,filename):
        image = Image.open(filename)
        #使用ImageEnhance可以增强图片的识别率
        #enhancer = ImageEnhance.Contrast(image)
        #enhancer = enhancer.enhance(4)
        image = image.convert(‘L‘)
        ltext = ‘‘
        ltext= image_to_string(image)
        #去掉非法字符,只保留字母数字
        ltext=re.sub("\W", "", ltext)
        print u‘[%s]识别到验证码:[%s]!!!‘ %(filename,ltext)
        image.save(filename)
        #print ltext
        return ltext

    def getRandomCode(self):
        #开始获取验证码
        #http://jbywcg.lnypcg.com.cn/CommonPage/Code.aspx?0.9409255818463862
        i = 0
        while ( i<=100):
            i += 1
            #拼接验证码Url
            randomUrlNew=‘%s/CommonPage/Code.aspx?%s‘ %(self.baseUrl,random.random())
            #拼接验证码本地文件名
            filename= ‘%s.png‘ %(i)
            filename=  os.path.join(self.curPath,filename)
            jpgdata,jpgheader = self.urllib2Navigate(randomUrlNew)
            if len(jpgdata)<= 0 :
                print u‘获取验证码出错!\n‘
                return False
            f = open(filename, ‘wb‘)
            f.write(jpgdata)
            #print u"保存图片:",fileName
            f.close()
            self.authCode = self.randomCodeOcr(filename)

#主程序开始
orcln=orclnypcg()
orcln.initUrllib2()
orcln.getRandomCode()

三、pytesseract代码优化

上述程序在windows平台运行时,会发现有黑色的控制台窗口一闪而过的画面,不太友好。

略微修改了pytesseract.py(C:\Python27\Lib\site-packages\pytesseract目录下),把上述过程进行了隐藏。

# modified by zhongtang hide console window
# new code
IS_WIN32 = ‘win32‘ in str(sys.platform).lower()
if IS_WIN32:
   startupinfo = subprocess.STARTUPINFO()
   startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
   startupinfo.wShowWindow = subprocess.SW_HIDE
   proc = subprocess.Popen(command,
        stderr=subprocess.PIPE,startupinfo=startupinfo)
‘‘‘
# old code
proc = subprocess.Popen(command,
   stderr=subprocess.PIPE)
‘‘‘
# modified end

为了方便初学者,把pytesseract.py也贴出来,高手自行忽略。

#!/usr/bin/env python
‘‘‘
Python-tesseract is an optical character recognition (OCR) tool for python.
That is, it will recognize and "read" the text embedded in images.

Python-tesseract is a wrapper for google‘s Tesseract-OCR
( http://code.google.com/p/tesseract-ocr/ ).  It is also useful as a
stand-alone invocation script to tesseract, as it can read all image types
supported by the Python Imaging Library, including jpeg, png, gif, bmp, tiff,
and others, whereas tesseract-ocr by default only supports tiff and bmp.
Additionally, if used as a script, Python-tesseract will print the recognized
text in stead of writing it to a file. Support for confidence estimates and
bounding box data is planned for future releases.

USAGE:
```
 > try:
 >     import Image
 > except ImportError:
 >     from PIL import Image
 > import pytesseract
 > print(pytesseract.image_to_string(Image.open(‘test.png‘)))
 > print(pytesseract.image_to_string(Image.open(‘test-european.jpg‘), lang=‘fra‘))
```

INSTALLATION:

Prerequisites:
* Python-tesseract requires python 2.5 or later or python 3.
* You will need the Python Imaging Library (PIL).  Under Debian/Ubuntu, this is
  the package "python-imaging" or "python3-imaging" for python3.
* Install google tesseract-ocr from http://code.google.com/p/tesseract-ocr/ .
  You must be able to invoke the tesseract command as "tesseract". If this
  isn‘t the case, for example because tesseract isn‘t in your PATH, you will
  have to change the "tesseract_cmd" variable at the top of ‘tesseract.py‘.
  Under Debian/Ubuntu you can use the package "tesseract-ocr".

Installing via pip:
See the [pytesseract package page](https://pypi.python.org/pypi/pytesseract)
$> sudo pip install pytesseract   

Installing from source:
$> git clone [email protected]:madmaze/pytesseract.git
$> sudo python setup.py install    

LICENSE:
Python-tesseract is released under the GPL v3.

CONTRIBUTERS:
- Originally written by [Samuel Hoffstaetter](https://github.com/hoffstaetter)
- [Juarez Bochi](https://github.com/jbochi)
- [Matthias Lee](https://github.com/madmaze)
- [Lars Kistner](https://github.com/Sr4l)

‘‘‘

# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
tesseract_cmd = ‘tesseract‘

try:
    import Image
except ImportError:
    from PIL import Image
import subprocess
import sys
import tempfile
import os
import shlex

__all__ = [‘image_to_string‘]

def run_tesseract(input_filename, output_filename_base, lang=None, boxes=False, config=None):
    ‘‘‘
    runs the command:
        `tesseract_cmd` `input_filename` `output_filename_base`

    returns the exit status of tesseract, as well as tesseract‘s stderr output

    ‘‘‘
    command = [tesseract_cmd, input_filename, output_filename_base]

    if lang is not None:
        command += [‘-l‘, lang]

    if boxes:
        command += [‘batch.nochop‘, ‘makebox‘]

    if config:
        command += shlex.split(config)

    # modified by zhongtang  hide console window
    # new code
    IS_WIN32 = ‘win32‘ in str(sys.platform).lower()
    if IS_WIN32:
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        startupinfo.wShowWindow = subprocess.SW_HIDE
    proc = subprocess.Popen(command,
            stderr=subprocess.PIPE,startupinfo=startupinfo)
    ‘‘‘
    # old code
    proc = subprocess.Popen(command,
            stderr=subprocess.PIPE)
    ‘‘‘
    # modified end

    return (proc.wait(), proc.stderr.read())

def cleanup(filename):
    ‘‘‘ tries to remove the given filename. Ignores non-existent files ‘‘‘
    try:
        os.remove(filename)
    except OSError:
        pass

def get_errors(error_string):
    ‘‘‘
    returns all lines in the error_string that start with the string "error"

    ‘‘‘

    lines = error_string.splitlines()
    error_lines = tuple(line for line in lines if line.find(‘Error‘) >= 0)
    if len(error_lines) > 0:
        return ‘\n‘.join(error_lines)
    else:
        return error_string.strip()

def tempnam():
    ‘‘‘ returns a temporary file-name ‘‘‘
    tmpfile = tempfile.NamedTemporaryFile(prefix="tess_")
    return tmpfile.name

class TesseractError(Exception):
    def __init__(self, status, message):
        self.status = status
        self.message = message
        self.args = (status, message)

def image_to_string(image, lang=None, boxes=False, config=None):
    ‘‘‘
    Runs tesseract on the specified image. First, the image is written to disk,
    and then the tesseract command is run on the image. Resseract‘s result is
    read, and the temporary files are erased.

    also supports boxes and config.

    if boxes=True
        "batch.nochop makebox" gets added to the tesseract call
    if config is set, the config gets appended to the command.
        ex: config="-psm 6"

    ‘‘‘

    if len(image.split()) == 4:
        # In case we have 4 channels, lets discard the Alpha.
        # Kind of a hack, should fix in the future some time.
        r, g, b, a = image.split()
        image = Image.merge("RGB", (r, g, b))

    input_file_name = ‘%s.bmp‘ % tempnam()
    output_file_name_base = tempnam()
    if not boxes:
        output_file_name = ‘%s.txt‘ % output_file_name_base
    else:
        output_file_name = ‘%s.box‘ % output_file_name_base
    try:
        image.save(input_file_name)
        status, error_string = run_tesseract(input_file_name,
                                             output_file_name_base,
                                             lang=lang,
                                             boxes=boxes,
                                             config=config)
        if status:
            #print ‘test‘ , status,error_string
            errors = get_errors(error_string)
            raise TesseractError(status, errors)
        f = open(output_file_name)
        try:
            return f.read().strip()
        finally:
            f.close()
    finally:
        cleanup(input_file_name)
        cleanup(output_file_name)

def main():
    if len(sys.argv) == 2:
        filename = sys.argv[1]
        try:
            image = Image.open(filename)
            if len(image.split()) == 4:
                # In case we have 4 channels, lets discard the Alpha.
                # Kind of a hack, should fix in the future some time.
                r, g, b, a = image.split()
                image = Image.merge("RGB", (r, g, b))
        except IOError:
            sys.stderr.write(‘ERROR: Could not open file "%s"\n‘ % filename)
            exit(1)
        print(image_to_string(image))
    elif len(sys.argv) == 4 and sys.argv[1] == ‘-l‘:
        lang = sys.argv[2]
        filename = sys.argv[3]
        try:
            image = Image.open(filename)
        except IOError:
            sys.stderr.write(‘ERROR: Could not open file "%s"\n‘ % filename)
            exit(1)
        print(image_to_string(image, lang=lang))
    else:
        sys.stderr.write(‘Usage: python pytesseract.py [-l language] input_file\n‘)
        exit(2)

if __name__ == ‘__main__‘:
    main()

以上……

时间: 2024-09-29 14:17:35

python下调用pytesseract识别某网站验证码的相关文章

Python下调用json.dumps中文显示问题解决办法

json.dumps在默认情况下,对于非ascii字符生成的是相对应的字符编码,而非原始字符,例如: import json js = json.loads('{"haha": "哈哈"}') print json.dumps(js) 得到的结果是{"haha": "\u54c8\u54c8"}, 中文字符打印出来的是utf-8转码...这个时候查血了docs.python.org,还有个参数ensure_ascii 调用了j

Python下调用Linux的Shell命令

http://blog.csdn.net/longerzone/article/details/17889969 python调用linux shell命令 使用os 模块及command模块的相关方法可以在python中调用linux shell命令. 有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的.那么我们使用Python如何调用Linux的Shell命令?下面来介绍几种常用的方法: 1. os 模块 1.1. os模块的exec方法族 Pyt

python利用selenium库识别点触验证码

利用selenium库和超级鹰识别点触验证码(学习于静谧大大的书,想自己整理一下思路) 一.超级鹰注册:超级鹰入口 1.首先注册一个超级鹰账号,然后在超级鹰免费测试地方可以关注公众号,领取1000积分,基本上就够学习使用了.如果想一直用可以用,可以充值,不是很贵. 2.下载超级鹰的python库代码.代码 3.然后有测试案例,自己可以试着跑一跑代码. 二.使用selenium库来识别点触式验证码: 1.首先是找一个使用点触式二维码的网站:(这个真的是比较难找了,由于静谧大大书上的网站被封了,我找

python下调用c语言代码

1)首先,创建一个.c文件,其大体内容如下: 2 #include <Python.h> 99 char * extract(char * path)                                                   //想要调用的函数100 {112     char * Q = (char * )malloc(3*sizeof(char));           . . .149     return Q;150 }151 152 153 PyObject

centos 下使用 pytesseract 识别文字

偶发一个想法搭一个验证码识别工具,网上查了一下有Tesseract 这个工具可以识别,所以有了后面一小时的搭建过程 ps:Ubuntu 下似乎可以直接用包管理工具来安装,我使用的源码编译安装 前提 由于自己电脑是工作用的,所以一些常用编译工具齐全,不这里介绍, 另外最好使用root 来编译 tesseract 依赖 leptonica, 而安装leptonica前前先安装常用图片库, 因为leptonica其实是对那些常用库进行了封装,如果编译时没有找到这个库,后面使用的时候就不会支持了 yum

Python下调用Linux的Shell命令的方法

下面来介绍几种常用的方法: 1. os 模块 1.1. os模块的exec方法族 Python的exec系统方法同Unix的exec系统调用是一致的.这些方法适用于在子进程中调用外部程序的情况,因为外部程序会替换当前进程的代码,不会返回.( 这个看了点 help(os)  --> search "exec" 的相关介绍,但是没太搞明白咋使用) 1.2. os模块的system方法 system方法会创建子进程运行外部程序,方法只返回外部程序的运行结果.这个方法比较适用于外部程序没

Python识别网站验证码

http://drops.wooyun.org/tips/6313 Python识别网站验证码 Manning · 2015/05/28 10:57 0x00 识别涉及技术 验证码识别涉及很多方面的内容.入手难度大,但是入手后,可拓展性又非常广泛,可玩性极强,成就感也很足. 验证码图像处理 验证码图像识别技术主要是操作图片内的像素点,通过对图片的像素点进行一系列的操作,最后输出验证码图像内的每个字符的文本矩阵. 读取图片 图片降噪 图片切割 图像文本输出 验证字符识别 验证码内的字符识别主要以机

Python 实现识别弱图片验证码

目前,很多网站为了防止爬虫肆意模拟浏览器登录,采用增加验证码的方式来拦截爬虫.验证码的形式有多种,最常见的就是图片验证码.其他验证码的形式有音频验证码,滑动验证码等.图片验证码越来越高级,识别难度也大幅提高,就算人为输入也经常会输错.本文主要讲解识别弱图片验证码. 1 图片验证码强度 图片验证码主要采用加干扰线.字符粘连.字符扭曲方式来增强识别难度. 加干扰线 加干扰线也分为两种,一种是线条跟字符同等颜色,另一种则线条的颜色是五颜六色. 字符粘连 各个字符之间的间隔比较小,互相依靠,能以分割.

Python验证码识别--利用pytesser识别简单图形验证码

一.探讨 识别图形验证码可以说是做爬虫的必修课,涉及到计算机图形学,机器学习,机器视觉,人工智能等等高深领域…… 简单地说,计算机图形学的主要研究内容就是研究如何在计算机中表示图形.以及利用计算机进行图形的计算.处理和显示的相关原理与算法.图形通常由点.线.面.体等几何元素和灰度.色彩.线型.线宽等非几何属性组成.计算机涉及到的几何图形处理一般有 2维到n维图形处理,边界区分,面积计算,体积计算,扭曲变形校正.对于颜色则有色彩空间的计算与转换,图形上色,阴影,色差处理等等. 在破解验证码中需要用