代码如下:

#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. ABP的数据过滤器(Data Filters)

    http://www.aspnetboilerplate.com/Pages/Documents/Data-Filters 我们在数据库开发中,一般会运用软删除 (soft delete)模式 ,即不 ...

  2. sqlite中的replace、insert、update之前的区别

    本文转自http://www.ithao123.cn/content-933827.html,在此感谢作者 android数据库操作,有两种方式,一种用android提供给我们的数据库操作函数inse ...

  3. Frament 方法

    public final boolean isAdded() 如果该Fragment对象被添加到了它的Activity中,那么它返回true,否则返回false. public final boole ...

  4. [转]MySQL主从复制原理介绍

    MySQL主从复制原理介绍 一.复制的原理 MySQL 复制基于主服务器在二进制日志中跟踪所有对数据库的更改(更新.删除等等).每个从服务器从主服务器接收主服务器已经记录到其二进制日志的保存的更新,以 ...

  5. Spring IoC小结

    一. IoC是什么 Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象 ...

  6. [译]GLUT教程 - glutPostRedisplay函数

    Lighthouse3d.com >> GLUT Tutorial >> Avoiding the Idle Func >> glutPostRedisplay 直 ...

  7. Jquery插件实现点击获取验证码后60秒内禁止重新获取

    通过jquery.cookie.js插件可以快速实现“点击获取验证码后60秒内禁止重新获取(防刷新)”的功能 先到官网(http://plugins.jquery.com/cookie/ )下载coo ...

  8. python 多线程编程

    这篇文章写的很棒http://blog.csdn.net/bravezhe/article/details/8585437 使用threading模块实现多线程编程一[综述] Python这门解释性语 ...

  9. 【原创】 SharePoint Service Unavaliable

    HTTP Error 503. The service is unavailable. 一般都是application pool停止工作了,停止原因可能是Identiy里的账号过期了什么的.

  10. 【ASP.NET Web API教程】6.4 模型验证

    本文是Web API系列教程的第6.4小节 6.4 Model Validation 6.4 模型验证 摘自:http://www.asp.net/web-api/overview/formats-a ...