代码如下:

#coding:utf-8
from PIL import Image
import pytesseract
def test():
im = Image.open(r"pic.gif")
vcode = pytesseract.image_to_string(im)
print vcode

执行以上代码进行简单验证码识别的时候会抛出一个异常:

Traceback (most recent call last):
File "D:\test\vcode.py", line 15, in <module>
main()
File "D:\test\vcode.py", line 9, in main
test()
File "D:\test\test.py", line 8, in test
vcode = pytesseract.image_to_string(im)
File "build\bdist.win32\egg\pytesseract\pytesseract.py", line 143, in image_to_string
File "D:\Program Files (x86)\Python\Python27\lib\site-packages\PIL\Image.py", line 1749, in split
self.load()
File "D:\Program Files (x86)\Python\Python27\lib\site-packages\PIL\ImageFile.py", line 232, in load
"(%d bytes not processed)" % len(b))
IOError: image file is truncated (5 bytes not processed)

解决办法是,再添加如下2句代码:

from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

最终,完整的代码如下:

#coding:utf-8
from PIL import Image
import pytesseract
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
def test():
im = Image.open(r"pic.gif")
vcode = pytesseract.image_to_string(im)
print vcode

相关文章:

关于利用python进行验证码识别的一些想法:http://www.cnblogs.com/xiaowuyi/archive/2012/09/10/2675286.html

python利用pytesser模块实现图片文字识别:http://www.jinglingshu.org/?p=9281

验证码图片字符识别两种python实现方法:http://vipscu.blog.163.com/blog/static/18180837220134234528457/

python模拟登陆登陆一:验证码与cookies的同步处理思路:http://www.dabu.info/python-login-crawler-captcha-cookies.html

原文地址:http://www.cnblogs.com/hongfei/p/4436767.html

Python:IOError: image file is truncated 的解决办法的更多相关文章

  1. [转]tftp在put上传的时候显示File not found的解决办法

    [转]tftp在put上传的时候显示File not found的解决办法 http://blog.163.com/pengcz%40126/blog/static/35908607201182433 ...

  2. 执行shell脚本时提示bad interpreter:No such file or directory的解决办法

    执行shell脚本时提示bad interpreter:No such file or directory的解决办法 故障现象:在终端直接cd /var正常,在shell脚本中执行则报错.原因是脚本是 ...

  3. VS2012与VS2015同时安装用VS2012创建MFC程序时弹出编译错误”fatal error C1083: 无法打开包括文件:“mprapidef.h”: No such file or directory”的解决办法

    在WIndows 7操作系统上同时安装VS2012与VS2015并用VS2012创建MFC程序时弹出编译错误”fatal error C1083: 无法打开包括文件:“mprapidef.h”: No ...

  4. UI进阶 XML解析适配 'libxml/tree.h'file not found 错误解决办法

    Xcode 'libxml/tree.h'file not found 错误解决办法

  5. 对于python中“FileNotFoundError: [Errno 2] No such file or directory”的解决办法

    在我们使用vscode运行Python代码时遇到的情况 一.出现原因:这里是由于Vscode中,python里的路径是相对与工作目录来进行定位的.所以在多级目录情况下,若不设置绝对路径,往往找不到相应 ...

  6. PYTHON -MYSQLDB安装遇到的问题和解决办法

    目前下载的mysqldb在window下没有exe安装包了,只有源码. 使用python setup.py install 命令安装, 报错如下: 异常信息如下: F:\devtools\MySQL- ...

  7. python程序一直在后台运行的解决办法

    刚写了个python程序,要一直在后台运行,即使断开ssh进程也在,下面是解决办法: 假如Python程序为test.py 编写shell脚本start.sh #!/bin/bash python t ...

  8. 【linux】CentOS安装mysql*.rpm提示conflicts with file from package的解决办法

    使用以下命令安装: rpm -ivh MySQL-server-5.6.19-1.linux_glibc2.5.x86_64.rpm 错误提示如下: Preparing...              ...

  9. [转]pro*c/c++编译错误 ” error: sqlca.h: No such file or directory “ 的解决办法

    $ gcc -o test test.c 出现错误:error: sqlca.h: No such file or directory [解决方法]知道 sqlca.h 在 $ORACLE_HOME/ ...

随机推荐

  1. canvas事件

    canvas的事件只能监听在canvas标签上,而不嗯那个想其他dom元素那样,所以针对canvas里面的图形监听事件是比较麻烦的. 通过isPointInPath(x,y);  这个方法用于判断某一 ...

  2. iOS 16进制字符串转换成int十进制

    NSRange rangeErr; rangeErr.location = 6; rangeErr.length = 2; NSString *strings = [value substringWi ...

  3. strace 监控所有php-fpm worker

    strace命令详解  http://linux.die.net/man/1/strace strace -tt -T $(pidof 'php-fpm: pool www' | sed 's/\([ ...

  4. Create My MySQL configuration by Percona

    本文地址:http://www.cnblogs.com/yhLinux/p/4013065.html https://tools.percona.com/ Percona是一款在线自动生成MySQL配 ...

  5. HibernateDaoSupport类的使用

    HibernateDaoSupport类的使用 1.        继承了HibernateDaoSupport类的类获取session时,已不可用SessionFactory.OpenSessioo ...

  6. IOS block使用中碰到的一个小坑

    1.先上段代码       __block typeof(self) tmpSelf = self; [tableview addLegendHeaderWithRefreshingBlock:^() ...

  7. Install nutch

    1. Install nutch on single node: $apt-get install subversion $apt-get install ant $svn co https://sv ...

  8. Adaboost算法初识

    1.算法思想很简单: AdaBoost 是一种迭代算法,其核心思想是针对同一个训练集训练不同的分类器,即弱分类器,然后把这些弱分类器集合起来,构造一个更强的最终分类器.(三个臭皮匠,顶个诸葛亮) 它的 ...

  9. MariaDB5.5(mysql)的partitioning设置 for Zabbix3.0

    用zabbix的同学都知道,一台服务器监视几百几千台服务器,一个服务器几十个item,长年下来数据量是很惊人的. 而zabbix自带的housekeeping功能,默认状态下的删除速度完全跟不上数据增 ...

  10. SQLSERVER拯救某个时间点被误删除的数据

    SQLSERVER拯救某个时间点被误删除的数据 转载自:http://blog.csdn.net/dba_huangzj/article/details/8491327 要拯救某个时间点被误删除的数据 ...