Tôi đang sử dụng ReportLab để in biểu đồ được tạo bởi matplotlib.IOError: "bộ giải mã zip không khả dụng" bằng cách sử dụng PNG matplotlib trong ReportLab trên Linux, hoạt động trên Windows
Tôi có thể thực hiện việc này trên máy phát triển Windows của mình mà không gặp sự cố nào. Tuy nhiên, khi tôi triển khai một máy chủ Ubuntu, kết xuất không thành công với lỗi được mô tả. Tôi cho rằng tôi thiếu một mô-đun Python, nhưng tôi không biết cái nào. Tôi tin rằng các phiên bản của Python, matplotlib, ReportLab và PIL giống nhau trên cả máy phát triển và máy chủ của tôi.
Mã để chuyển đổi hình matplotlib (gọi là biểu đồ) để PNG và gửi lại:
img_stream = StringIO.StringIO()
chart.savefig(img_stream, format = 'png')
img_stream.seek(0)
return img_stream
Mã để sử dụng hình ảnh:
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')
Khi chạy trên Windows, hoạt động này. Khi chạy trên Linux, lỗi này tạo ra lỗi này:
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=[[email protected]]
handle_pageBegin args=()
Có vẻ như bạn đang thiếu mô-đun zlib. Hãy xem tại đây http://www.foxhop.net/ubuntu-python-easy_install-pil-does-not-install-zlib-support – RanRag
Bạn có tự biên dịch PIL không? Hoặc là PIL được quản lý bởi 'apt'? – sarnold
@sarnold: Tôi tin PIL đã được ReportLab cài đặt. Trong mọi trường hợp, tôi đã không xây dựng nó, tôi đã nhận nó hoặc là một phần của cái gì khác hoặc với easy_install trên Windows và apt-get trên Ubuntu. –