【发布时间】:2012-01-31 01:55:00
【问题描述】:
我正在使用 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=()
【问题讨论】:
-
您似乎缺少 zlib 模块。看看这里foxhop.net/…
-
你自己编译过PIL吗?还是 PIL 由
apt管理? -
@sarnold:我相信 PIL 是由 ReportLab 安装的。无论如何,我没有构建它,我将它作为其他东西的一部分或在 Windows 上使用 easy_install 和在 Ubuntu 上使用 apt-get 获得。
-
@RanRag:我能够使用这些说明来解决问题(必须在符号链接 zlib 的 so 后重新安装 PIL。请作为答案发布,以便您可以要求您的积分(以及有相同问题的下一个人可以找到答案)。
-
@LarryLustig :如果您觉得有任何遗漏或错误,请编辑它。
标签: python png python-imaging-library reportlab