• Python处理验证码第一篇(pytesser初探及报错处理)


    (原文链接:http://blog.csdn.net/Bone_ACE/article/details/50436587

    前言:

    春节期间,无法全身心投入地去写爬虫,那就玩玩验证码吧,应该比较有趣!

    首次接触验证码识别,用pytesser接触一下最简单的验证码先,代码参照:使用python以及工具包进行简单的验证码识别。具体细节可以参见原文,里面安装和报错处理没有详细记录,我在此处主要记录一下自己的安装及处理过程。 

    效果:

    可识别以下类型的验证码: 
    这里写图片描述 这里写图片描述 这里写图片描述 这里写图片描述 

    正文:

    代码:

    # encoding=utf-8
    from PIL import Image
    from pytesser import *
    
    img = Image.open('验证码.jpg')
    img_grey = img.convert('L')
    
    threshold = 140
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    img_out = img_grey.point(table, '1')
    
    text = image_to_string(img_grey)  # 将图片转成字符串
    print text
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    安装包:

    需要安装的包主要有两个: PIL 和 pytesser 。 
    我的环境:64位win8系统、python2.7

    PIL模块的安装:

    PIL 全称 “Python Imaging Library”。 
    下载地址:传送门

    我下载了这里写图片描述

    运行报错:Python version 2.7 required, which was not found in the registry. 
    Python version 2.7 required, which was not found in the registry.

    网上找出原因:这个 PIL 安装资源是给32位操作系统,32位和64位操作系统下的python在注册表中的路径是不一样的: 
    64位检查注册表的位置是: HKLM|HKCUSOFTWARE 
    32位检查注册表的位置是: HKLM|HKCUSOFTWAREwow6432node 
    所以在64位系统中自然检查不出有Python。

    解决方案: 
    官方没有提供64位的PIL库,但非官方有:传送门。 
    下载里面的pillow库: 
    64位PIL
    然后在命令行安装该模块: 
    pip install G:/For_Life/360download/Pillow-3.1.1-cp27-none-win_amd64.whl

    注意:在 pillow 下载页中有一行 “Use ‘from PIL import Image’ instead of ‘import Image’” ,所以一般方法安装的 PIL 要导入 Image 模块使用 “import Image” ,而此方法安装的 PIL 要使用 “from PIL import Image” 。 
    更多请见:Windows安装Python图像处理库:PIL模块

    pytesser模块的安装:

    下载地址:传送门。(此网址有时候会加载失败,我将自己下载的放在微盘上了:传送门 密码:DPHE)

    下载后得到 “pytesser_v0.0.1.zip”,是一个压缩文件,使用方法: 
    1、在 “D:For_SoftwareothersPython_PyDevPythonLibsite-packages” 路径下新建一个文件夹,命名 “pytesser” 。把 “pytesser_v0.0.1.zip” 里的文件解压到该目录: 
    pytesser模块的安装

    2、将 “pytesser.py” 改名为 “__init__.py”。

    3、打开 “__init__.py” 文件,将 “tesseract_exe_name” 变量的值改为 “‘D:/For_Software/others/Python_PyDev/Python/Lib/site-packages/pytesser/tesseract’”(原值为 “‘tesseract’”)。

    4、pytesser 模块依赖于 PIL 模块,如果是按照上面的方法安装 PIL 的话,需要把 “init.py” 文件里的 “import Image” 改成 “from PIL import Image” 。 
    (注意以上的路径要改成自己的路径,不要原搬照抄,应该不会这么笨吧)

    如果报错:WindowsError: [Error 2] 
    pytesser WindowsError:[Error 2] 
    可能的原因:python找不到 “tesseract.exe”,执行步骤3给python指定一个绝对路径就可以了。

    如果报错:ImportError: No module named Image 
    pytesser ImportError: No module named Image 
    可能的原因:在 pytesser 中的 “__init__.py” ,导入 Image 的方式不对,见步骤4。 

    总结:

    其实这只是处理最简单的验证码而已,代码也很简单,主要做两个工作而已。第一个是将彩色图片转成灰度图片并除噪,第二个是使用 pytesser.image_to_string() 将图片中的字符提取出来。

    处理的图片也是非常有限的图片上的数字或字母必须没有变形(即使是没有变形的情况下还会出现数字跟字母混淆的情况),验证码的背景不能太复杂,允许字体加上一些颜色。 
    这么简单的验证码估计很少见,不常用。所以这么简单的处理方法,我们看一看也就可以了,如果实际使用的话技术还有待深入。


    转载请注明出处,谢谢!(原文链接:http://blog.csdn.net/Bone_ACE/article/details/50436587

  • 相关阅读:
    浏览器检测
    EcmaScript基础
    js中的内置对象
    cursor 与refcursor及sys_refcursor的区别 (转载)
    各种连接数据方法的网站
    UVa11627 Slalom
    UVa1450 Airport
    UVa12124 Assemble
    UVa11384 Help is needed for Dexter
    UVa11464 Even Parity
  • 原文地址:https://www.cnblogs.com/Devopser/p/6405846.html
Copyright © 2020-2023  润新知