代码如下:

#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. LeetCode(四)

    Find Kth Largest Number public class Solution { public int findKthLargest(int[] nums, int k) { retur ...

  2. 内存,堆,栈,heap,stack,data

    1. 基本类型占一块内存. 引用类型占两块. 2. 类是静态概念. 函数中定义的基本类型变量和对象的引用类型变量都在函数的栈内存. 局部变量存在栈内存. new创建的对象和数组,存在堆内存. java ...

  3. [python] 线程简介

    参考:http://www.cnblogs.com/aylin/p/5601969.html 我是搬运工,特别感谢张岩林老师! python 线程与进程简介 进程与线程的历史 我们都知道计算机是由硬件 ...

  4. Spring+Struts2/Hibernate 学习笔记

    ============Spring与Struts2整合============ (1)拷JAR包(Spring.Struts2) (2)配置org.springframework.web.conte ...

  5. css之字体设置

    字体大小font-size 字体风格font-style 字体加粗font-weight 字体类型font-family 字体阴影text-shadow 字体行高line-height 字间距lett ...

  6. Json&Razor&控制器

    JsonJson 属于JavaScript所以要书写在<script></script>中1.语法规则: 1.1:键值对 1.2:逗号分隔 1.3:花括号保存对象 1.4:方括 ...

  7. [转载]理解HTML语义化

    声明: 本文转载于:freeyiyi1993博客. 原文地址:http://www.cnblogs.com/freeyiyi1993/p/3615179.html 1.什么是HTML语义化? < ...

  8. 斯坦福第十七课:大规模机器学习(Large Scale Machine Learning)

    17.1  大型数据集的学习 17.2  随机梯度下降法 17.3  微型批量梯度下降 17.4  随机梯度下降收敛 17.5  在线学习 17.6  映射化简和数据并行 17.1  大型数据集的学习

  9. CentOS下 pycharm开发环境搭建之无穷无尽的问题

    在上一篇的环境搭建中,表面上以为已经升级好python,安装好pycharm,并且可以用上了django框架,谁知道,谁知道,又是一断被虐的经历,我都要快恼羞成怒了. 在些记录一下我的经历. 1.首先 ...

  10. Common Scenarios to avoid with DataWarehousing

    Database Design Rule Description Value Source Problem Description 1 Excessive sorting and RID lookup ...