IOError:“解码器zip不可用";在Linux上的ReportLab中使用matplotlib PNG,可在Windows上使用 [英] IOError: "decoder zip not available" using matplotlib PNG in ReportLab on Linux, works on Windows

查看:119
本文介绍了IOError:“解码器zip不可用";在Linux上的ReportLab中使用matplotlib PNG,可在Windows上使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ReportLab打印由matplotlib生成的图表.

I'm using ReportLab to print a chart produced by matplotlib.

我可以在Windows开发计算机上执行此操作,而不会遇到麻烦.但是,当我部署到Ubuntu服务器时,渲染失败并显示错误.我以为我缺少Python模块,但我不知道哪个模块.我相信我的开发机器和服务器上的Python,matplotlib,ReportLab和PIL的版本都相同.

I'm able to do this on my Windows development machine without trouble. When I deploy to a Ubuntu server, however, the rendering fails with the error described. I assume I'm missing a Python module, but I don't know which one. I believe the versions of Python, matplotlib, ReportLab and PIL are the same on both my development machine and the server.

将matplotlib图形(称为图表)转换为PNG并将其返回的代码:

Code to convert the matplotlib figure (called chart) to PNG and return it:

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上运行时,会产生此错误:

When run on Windows, this works. When run on Linux it produces this error:

  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=()

推荐答案

显然,PIL setup.py不知道如何找到libz.so. PIL期望libz.so位于/usr/lib而不是/usr/lib/i386-linux-gnu/libz.so中.

Apparently PIL setup.py doesn't know how to find libz.so. PIL expects libz.so to be located in /usr/lib not /usr/lib/i386-linux-gnu/libz.so.

解决问题

1)使用find . -name libz.so查找系统libz.so的位置.

1) Find the location of your systems libz.so using find . -name libz.so.

2)使用sudo ln -s /usr/lib/i386-linux-gnu/libz.so /usr/lib创建从libz.so到/usr/lib的软链接.

2) Create a soft link from libz.so to /usr/lib using sudo ln -s /usr/lib/i386-linux-gnu/libz.so /usr/lib.

3)正如@Larry所建议的那样,您必须在对zlib进行so链接之后重新安装PIL.

3) And as @Larry suggested you had to reinstall PIL after sym-linking the so for zlib.

要解决64位系统的此问题,请在此处查看 http://www.foxhop.net/ubuntu-python-easy_install-pil-does-not-install-zlib-support

To solve this problem for 64-bit system take a look here http://www.foxhop.net/ubuntu-python-easy_install-pil-does-not-install-zlib-support

这篇关于IOError:“解码器zip不可用";在Linux上的ReportLab中使用matplotlib PNG,可在Windows上使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆