Python:IOError: image file is truncated 的解决办法

代码如下:

#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/

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

时间: 2024-08-05 15:18:55

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

Python IOError: [Errno 22] invalid mode (&#39;r&#39;) 解决方法

O'Reilly出版的Wes McKinney编的<Python for Data Analysis> 本人使用工具: PyCharm 2016.3.2 rnames =['user_id','movie_id','rating','time_cd'] ratings = pd.read_table('data\ml-1m\ratings.dat',sep='::',header=None,names=rnames) 报错信息如下: 原因:\ratings.dat 中的 "\r&qu

python安装markupsafe模块时卡死的解决办法

起因: 升级OS X从10.8到10.9,会发现在安装python的markupsafe模块时一直卡住. 当时的机器环境是: OSX 10.9, XCode 4.6.2, Python 2.7.6, Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn) 界面一直停留在下面的情况 mbp:MarkupSafe-0.23 $ python setup.py install running install running bdis

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

目前下载的mysqldb在window下没有exe安装包了,只有源码. 使用python setup.py install 命令安装, 报错如下: 异常信息如下: F:\devtools\MySQL-python-1.2.3>pythonsetup.py build Traceback (most recent call last): File "setup.py", line 15, in <module> metadata, options = get_confi

XML解析适配 &#39;libxml/tree.h&#39;file not found 错误解决办法

Xcode 'libxml/tree.h'file not found 错误解决办法 在工程的"Build Settings"页中找到"Header Search Path"项,添加"/usr/include/libxml2".在"Other Linker Flags"项,添加"-lxml2" XML解析适配 'libxml/tree.h'file not found 错误解决办法

UI进阶 XML解析适配 &#39;libxml/tree.h&#39;file not found 错误解决办法

Xcode 'libxml/tree.h'file not found 错误解决办法 在工程的"Build Settings"页中找到"Header Search Path"项,添加"/usr/include/libxml2".在"Other Linker Flags"项,添加"-lxml2" UI进阶 XML解析适配 'libxml/tree.h'file not found 错误解决办法

UI进阶 XML解析适配 引入GDataXML文件时候 &#39;libxml/tree.h&#39;file not found 错误解决办法

在工程的"Build Settings"页中找到"Header Search Path"项,添加"/usr/include/libxml2". 在"Other Linker Flags"项,添加"-lxml2" 同理引入别的类型文件不匹配的时候也可以用这种方法 UI进阶 XML解析适配 引入GDataXML文件时候 'libxml/tree.h'file not found 错误解决办法

Failed to issue method call: Unit mysql.service failed to load: No such file or directory的解决办法

Failed to issue method call: Unit mysql.service failed to load: No such file or directory的解决办法 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 1.软件环境: OpenSUSE 13.1 x64 MySQL 5.6.20 x64 2.采用RPM包安装MySQL 5.6.20 # rpm -ivh MySQL-server-5.6.20-1.sles11.x8

Error: Cannot open main configuration file &#39;//start&#39; for reading! 解决办法

当执行service nagios start启动nagios时,报错:Error: Cannot open main configuration file '//start' for reading! 此问题是修改了nagios安装源文件导致的报错.如报此问题,请执行如下命令,便可解决: /usr/bin/install -c -m 755 -d -o root -g root /etc/rc.d/init.d /usr/bin/install -c -m 755 -o root -g roo

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 such file or directory"的解决办法: I installed vs2015 and vs2012, then the same problem occurs,however, I find a solution. open visual stud