IOError:“解码器 zip 不可用”在 Linux 上的 ReportLab 中使用 matplotlib PNG,适用于 Windows
我正在使用 ReportLab 打印 matplotlib 生成的图表。
我可以在我的 Windows 开发机器上毫无问题地执行此操作。然而,当我部署到 Ubuntu 服务器时,渲染失败并出现所述错误。我假设我缺少一个 Python 模块,但我不知道是哪一个。我相信我的开发机器和服务器上的 Python、matplotlib、ReportLab 和 PIL 的版本是相同的。
将 matplotlib 图形(称为图表)转换为 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=()
I'm using ReportLab to print a chart produced by matplotlib.
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.
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
Code to use the image:
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')
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=()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然 PIL setup.py 不知道如何找到 libz.so。 PIL 希望
libz.so
位于/usr/lib
中,而不是/usr/lib/i386-linux-gnu/libz.so
中。解决此问题
1) 使用
find 查找系统 libz.so 的位置。 -名称 libz.so
。2) 使用 sudo ln -s /usr/lib/i386-linux-gnu/libz.so /usr/lib 创建从 libz.so 到 /usr/lib 的软链接。
3)正如@Larry建议的那样,您必须在对zlib的so进行符号链接后重新安装PIL。
要解决 64 位系统的此问题,请查看此处 http://www.foxhop.net/ubuntu-python-easy_install-pil-does-not-install-zlib-support">http:// /www.foxhop.net/ubuntu-python-easy_install-pil-does-not-install-zlib-support
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
.To fix the problem
1) Find the location of your systems libz.so using
find . -name libz.so
.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) And as @Larry suggested you had to reinstall PIL after sym-linking the so for zlib.
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