IOError: 解码器 zip 不可用" : Ubuntu Python PIL
我正在尝试运行这个示例代码 heatmap.py,地址是:http://jjguy.com/heatmap/
#image.py
import heatmap
import random
if __name__ == "__main__":
pts = []
for x in range(400):
pts.append((random.random(), random.random() ))
print "Processing %d points..." % len(pts)
hm = heatmap.Heatmap()
img = hm.heatmap(pts)
img.save("classic.png")
但是我遇到了这个错误:
Processing 400 points...
Traceback (most recent call last):
File "/home/ec2usr/workspace/image/image.py", line 14, in <module>
img.save("classic.png")
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 1437, in save
save_handler(self, fp, filename)
File "/usr/local/lib/python2.7/dist-packages/PIL/PngImagePlugin.py", line 572, in _save
ImageFile._save(im, _idat(fp, chunk), [("zip", (0,0)+im.size, 0, rawmode)])
File "/usr/local/lib/python2.7/dist-packages/PIL/ImageFile.py", line 481, in _save
e = Image._getencoder(im.mode, e, a, im.encoderconfig)
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 399, in _getencoder
raise IOError("encoder %s not available" % encoder_name)
IOError: encoder zip not available
我是在 Ubuntu 12(64位)系统上用 Eclipse 工作,使用的是 Python 2.7。
我在 /usr/lib 和 /usr/lib/x86_64-linux-gnu/ 这两个地方都找到了 libz.so,没问题。可是我已经尝试了以下解决方案,但都没有效果:
PIL 说它有“可用支持”,但在保存文件时仍然出现 IOError
关于 PIL 错误 -- IOError: 解码器 zip 不可用
IOError: “解码器 zip 不可用”,在 Linux 上使用 matplotlib PNG 在 ReportLab 中,Windows 上可以工作
http://www.foxhop.net/ubuntu-python-easy_install-pil-does-not-install-zlib-support
我为这个问题烦恼了好几天,非常感谢任何人的帮助!
来自 python-imaging 的安装日志:
ubuntu@ip-10-241-17-21:/usr/lib$ sudo apt-get install python-imaging
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
account-plugin-identica account-plugin-twitter gir1.2-messagingmenu-1.0
hplip-data libgtkspell-3-0 libqt4-designer libqt4-help libqt4-scripttools
libqt4-svg libqt4-test libqtassistantclient4 libsane-hpaio
linux-headers-3.5.0-21 linux-headers-3.5.0-21-generic python-debtagshw
python-lxml python-pexpect python-piston-mini-client python-qt4
python-renderpm python-reportlab python-reportlab-accel python-sip
software-center-aptdaemon-plugins ubuntu-extras-keyring
Use 'apt-get autoremove' to remove them.
Suggested packages:
python-imaging-doc python-imaging-dbg
The following NEW packages will be installed:
python-imaging
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/294 kB of archives.
After this operation, 996 kB of additional disk space will be used.
Selecting previously unselected package python-imaging.
(Reading database ... 189302 files and directories currently installed.)
Unpacking python-imaging (from .../python-imaging_1.1.7-4build1_amd64.deb) ...
Setting up python-imaging (1.1.7-4build1) ...
ubuntu@ip-10-241-17-21:/usr/lib$
安装日志(从 Imaging 运行 python install setup.py):
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version 1.1.7
platform linux2 2.7.3 (default, Sep 26 2012, 21:51:14)
[GCC 4.7.2]
--------------------------------------------------------------------
--- TKINTER support available
*** JPEG support not available
--- ZLIB (PNG/ZIP) support available
*** FREETYPE2 support not available
*** LITTLECMS support not available
selftest.py:
--------------------------------------------------------------------
PIL 1.1.7 TEST SUMMARY
--------------------------------------------------------------------
Python modules loaded from ./PIL
Binary modules loaded from /usr/local/lib/python2.7/dist-packages
--------------------------------------------------------------------
*** PIL CORE support not installed
*** TKINTER support not installed
--- JPEG support ok
--- ZLIB (PNG/ZIP) support ok
*** FREETYPE2 support not installed
*** LITTLECMS support not installed
--------------------------------------------------------------------
5 个回答
虽然我的PIL(Python Imaging Library)是最新的,但我在Ubuntu 12.04上通过以下方式解决了这个问题:
pip uninstall PIL
pip install PIL
我记得在64位系统上安装PIL时,得到了和Kris完全一样的解决方案。
不过现在我建议用Pillow(通过命令pip install pillow)来代替PIL。Pillow其实是PIL的一个分支,更新更频繁,问题也比你遇到的少。
如果你使用的是Ubuntu系统,我觉得在安装PIL/Pillow之前,你需要先安装zlib1g-dev和libjpeg-dev,这样才能支持jpeg/png格式。
我刚遇到一个类似的问题,是因为我同时安装了PIL(通过python的pip安装)和通过apt-get安装的python-imaging包。删除了pip里的那个多余版本后,问题就解决了。
如果你是从源代码安装PIL或者用pip安装的,那么你也可能会遇到同样的问题。
在我构建PIL的时候,我还发现需要把库链接到/usr/lib目录。这样可能会解决你日志中出现的“不支持”的信息。
sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib
sudo ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib
sudo ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/lib