Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOError: "decoder zip not available" using matplotlib PNG in ReportLab on Linux, works on Windows

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=()
like image 906
Larry Lustig Avatar asked Jan 31 '12 01:01

Larry Lustig


1 Answers

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

like image 198
RanRag Avatar answered Nov 15 '22 16:11

RanRag