IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

pytesser:图片验证码识别

雕刻时光 2012-05-28 12:31:27 累计浏览 4,260 次
本机暂存

pytesser是一个用于图片文本识别的python模块:http://code.google.com/p/pytesser/,即从文本的截图中还原出文本信息;

网上在windows上安装、使用的资料比较多,而没有linux的资料;

作者虽然没有说明pytesser在linux环境下测试过,但也表示“The scripts should work in Linux as well.”;

今天在我的ubuntu9.10上编译、安装、使用了一把,过程中遇到一些问题并解决,记在这里:

  1. pytesser依赖于PIL,因此需要先安装PIL模块,详见:http://wenyue.me/blog/278
  2. pytesser调用了tesseract,因此需要安装tesseract:
    先用包管理器安装这几个库:
    1
    2
    3
    4
    sudo apt-get install libpng12-dev
    sudo apt-get install libjpeg62-dev
    sudo apt-get install libtiff4-dev
    sudo apt-get install zlibg-dev

    下载tesseract的源码包:http://tesseract-ocr.googlecode.com/files/tesseract-3.00.tar.gz
    解压、cd到解压后目录下tesseract-3.00/
    运行./configure -prefix=你想要安装到的路径,比如:

    1
    ./configure --prefix=/home/pf-miles/installation/install/tesseract

    然后make & make install
    将tesseract的运行脚本加到环境变量中,比如:

    1
    export PATH=$PATH:/home/pf-miles/installation/install/tesseract/bin

    , 这个路径与刚才你configure的时候设置的路径有关
    http://code.google.com/p/tesseract-ocr/downloads/list页 面去下载最新的eng.traineddata.gz文件,解压后的eng.traineddata放到/home/pf-miles /installation/install/tesseract/share/tessdata目录下,注意,虽然tesseract的svn trunk里也有这个文件,但那个用不得,会报

    1
    actual_tessdata_num_entries_ <= TESSDATA_NUM_ENTRIES:Error:Assert failed:in file tessdatamanager.cpp, line 55

    错误,详见:http://www.uluga.ubuntuforums.org/showthread.php?p=10248384,所以一定要用http://code.google.com/p/tesseract-ocr/downloads/list这里下载的那一份
    试一试:

    1
    2
    pf-miles@pf-miles-desktop:~/downloads$ tesseract
    Usage:tesseract imagename outputbase [-l lang] [configfile [[+|-]varfile]...]

    OK,tesseract安装完毕

  3. 下载pytesser包:http://pytesser.googlecode.com/files/pytesser_v0.0.1.zip(目前是0.0.1版本), 解压…并cd到解压后的目录下
  4. 目录下有个“phototest.tif”图片文件作为测试用,直接在目录下写一个python脚本进行测试:
    test.py:
    1
    2
    3
    4
    from pytesser import *
    im = Image.open('phototest.tif')
    text = image_to_string(im)
    print text

    运行:

    1
    pf-miles@pf-miles-desktop:~/downloads/pytesser$ python test.py 2>/dev/null

    结果:
    Thls IS a lot of 12 pornt text to test the
    ocr code and see lf It works on all types
    of frle format
    lazy fox The qurck brown dog jumped
    over the lazy fox The qulck brown dog
    jumped over the lazy fox The QUICK
    brown dog jumped over the lazy fox
    The quick brown dog jumped over the

应该说准确率还令人满意吧.

pytesser的验证码识别能力比较低,只能对规规矩矩不歪不斜数字和字母验证码进行识别,这里还是要介绍下它的用法。有关它的安装和python对应的模块可以参考http://wenyue.me/blog/tag/pytesser

pytesser只能对tiff(tif)格式的图片文件进行识别,大部分网站的验证码图片不是tiff格式的,所以需要进行转换。

可使用Image模块转化图片格式

#需要保存成tmp.tiff,发现保存成tmp.tif的话pytesser无法识别

Image.open(‘tmp.gif’).convert(‘RGB’).save(‘tmp.tiff’)

获取验证码的时候需要让对方服务器写如cookie,所以需要以下这段

jk = cookielib.LWPCookieJar()

cookies = urllib2.HTTPCookieProcessor(jk)

opener = urllib2.build_opener(cookies)

然后再需要拿着这个opener去登录, 登录成功后的,再去请求其他需要登录的页面的时候也需要使用这个opener去urlopen

同分类推荐文章

  1. 从”内容治理”到”行为治理”:中国智能体治理框架深度解析与绿盟科技实践 (2026-06-23 21:49:28)
  2. 美团海报生成 AIGC 技术创新与实践 (2026-06-22 15:34:28)
  3. AI Coding Agent 时代,我自己最常用的 4 个终端工具 (2026-06-22 08:00:00)

查看更多 AI 文章 →

建议继续学习

  1. 用Hyer来进行网站的抓取 (累计阅读 158,251)
  2. 配置Nginx+uwsgi更方便地部署python应用 (累计阅读 107,164)
  3. 程序员技术练级攻略 (累计阅读 35,469)
  4. python实现自动登录discuz论坛 (累计阅读 32,834)
  5. python编程细节──遍历dict的两种方法比较 (累计阅读 20,371)
  6. 每个程序员都应该学习使用Python或Ruby (累计阅读 17,918)
  7. Chrome和goagent的配置方法,你懂的 (累计阅读 16,842)
  8. 30分钟3300%性能提升――python+memcached网页优化小记 (累计阅读 13,742)
  9. 使用python爬虫抓站的一些技巧总结:进阶篇 (累计阅读 13,301)
  10. 我的PHP,Python和Ruby之路 (累计阅读 13,146)