sci.misc.imread读取数据为空 或者 IOError: decoder zip not available decoder jpeg not available 解决办法

来源:互联网 发布:淘宝一手货源一件代发 编辑:程序博客网 时间:2024/06/05 08:25

一. 问题描述    

         前几天centOS服务器突然出现以下问题:

from scipy.misc import imreadimg= imread(jpg_file)img.shape

        返回:

()
        输入:

img
        返回:

array(<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=870x1110 at 0x10BEC3F80>, dtype=object)

        试着输入:

img= imread(jpg_file, 1)
        读入灰度图像,则会报错:
 IOError: decoder jpeg not available

        如果是png图像,则会报错:

 IOError: decoder zip not available 
   

二. 解决方法   

        在其它服务器上运行,则会正确输出img.shape以及img的数据。刚开始以为scipy安装出现了问题,重装以后问题仍在。后来根据网上一些帖子,又按照本服务器的配置,终于解决了。

        步骤:

      (1)卸载PIL        

pip uninstall PIL 
         如果觉得没有卸载彻底,可以再执行以下命令,删除残余文件:

rm -rf /usr/lib64/python2.6/site-packages/PILrm /usr/lib64/python2.6/site-packages/pil.pth
      (2) 加入必须的系统库
yum install zlib zlib-devel yum install libjpeg libjpeg-level yum install freetype freetype-devel 
      (3)重新安装PIL

        网上有采用:

pip install PIL
       但是这种方法会出现以下错误:

--- TKINTER support available*** JPEG support not available*** ZLIB (PNG/ZIP) support not available*** FREETYPE2 support not available*** LITTLECMS support not available
       建议采用下载安装包的方式来重装PIL
wget http://effbot.org/downloads/Imaging-1.1.7.tar.gztar xvfz Imaging-1.1.7.tar.gzcd Imaging-1.1.7python setup.py build_ext -i
       输入上述命令,会有以下输出:

PIL 1.1.7 SETUP SUMMARY--------------------------------------------------------------------version       1.1.7platform      linux2 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)              [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]----------------------------------------------------------------------- TKINTER support available--- JPEG support available--- ZLIB (PNG/ZIP) support available--- FREETYPE2 support available*** LITTLECMS support not available--------------------------------------------------------------------To add a missing option, make sure you have the requiredlibrary, and set the corresponding ROOT variable in thesetup.py script.To check the build, run the selftest.py script.
      TKINTER和LITTLECMS 可能出现不可用的情况,但是不影响PIL的使用。

      最后输入:

python setup.py install
      重新测试问题示例,img.shape则会正确显示:

(870, 1110, 3)
      大功告成。

    

      PS:

      1. 在第三步重新安装PIL时,有教程说需要修改setup.py的相关路径:    

TCL_ROOT = "/usr/lib64/"JPEG_ROOT = "/usr/lib64/"ZLIB_ROOT = "/usr/lib64/"TIFF_ROOT = "/usr/lib64/"FREETYPE_ROOT = "/usr/lib64/"LCMS_ROOT = "/usr/lib64/"
但是这项更改,对于本机无效。如果你使用默认的路径无效,不妨参照这里 的结尾部分来修改这几个路径。
     

     2. 由于之前服务器上安装了两个python版本,导致pip 等组件较为混乱,无法正常使用。给配置工作带来很多麻烦,以后电脑上最好只安装一个版本的python。

参考链接:

http://www.laonan.net/blog/57/

http://www.cnblogs.com/neolf/articles/2409118.html

http://www.yihaomen.com/article/python/286.htm

0 0
原创粉丝点击