IOError: "解码器 zip 不可用" 在Linux上使用matplotlib PNG于ReportLab,Windows上正常
我正在使用ReportLab来打印一个由matplotlib生成的图表。
在我的Windows开发机器上,这个过程很顺利。但是,当我把它部署到Ubuntu服务器上时,渲染就失败了,并出现了描述的错误。我猜可能是缺少某个Python模块,但我不知道具体是哪个。我相信我的开发机器和服务器上的Python、matplotlib、ReportLab和PIL的版本都是一样的。
下面是将matplotlib图形(叫做chart)转换为PNG并返回的代码:
img_stream = StringIO.StringIO()
chart.savefig(img_stream, format = 'png')
img_stream.seek(0)
return img_stream
下面是使用这个图像的代码:
res_img = charts.CreateProjectionChart(doc.fund) #calls above code
if res_img:
img = ImageReader(res_img)
canvas.drawImage(img, FromLeft(first_col), FromTop(3.5, 2), width - (.1 * inch), 1.75 * inch, preserveAspectRatio=True, anchor='c')
在Windows上运行时,这段代码可以正常工作。但在Linux上运行时却出现了这个错误:
File "/home/web-server/reports.py", line 913, in FirstPageSetup
canvas.drawImage(img, FromLeft(first_col), FromTop(3.5, 2), width - (.1 * inch), 1.75 * inch, preserveAspectRatio=True, anchor='c')
File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/pdfgen/canvas.py", line 840, in drawImage
rawdata = image.getRGBData()
File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/lib/utils.py", line 658, in getRGBData
annotateException('\nidentity=%s'%self.identity())
File "/usr/local/lib/python2.7/dist-packages/reportlab-2.5-py2.7-linux-x86_64.egg/reportlab/lib/utils.py", line 648, in getRGBData
if Image.VERSION.startswith('1.1.7'): im.load()
File "/usr/local/lib/python2.7/dist-packages/PIL-1.1.7-py2.7-linux-x86_64.egg/ImageFile.py", line 189, in load
d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
File "/usr/local/lib/python2.7/dist-packages/PIL-1.1.7-py2.7-linux-x86_64.egg/Image.py", line 385, in _getdecoder
raise IOError("decoder %s not available" % decoder_name)
IOError: decoder zip not available
identity=[ImageReader@0x30336d0]
handle_pageBegin args=()
1 个回答
12
看起来PIL的setup.py找不到libz.so这个文件。PIL希望在/usr/lib
这个地方找到libz.so
,而不是在/usr/lib/i386-linux-gnu/libz.so
。
要解决这个问题,你可以按照以下步骤操作:
1) 使用find . -name libz.so
命令找到你系统中libz.so的具体位置。
2) 使用sudo ln -s /usr/lib/i386-linux-gnu/libz.so /usr/lib
命令创建一个软链接,把libz.so链接到/usr/lib目录下。
3) 正如@Larry建议的那样,在创建了zlib的软链接后,你需要重新安装PIL。
如果你是64位系统,可以参考这里的内容:http://www.foxhop.net/ubuntu-python-easy_install-pil-does-not-install-zlib-support