7

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=()
Larry Lustig
  • 49,320
  • 14
  • 110
  • 160
  • 1
    It seems you are missing zlib module. Take a look here http://www.foxhop.net/ubuntu-python-easy_install-pil-does-not-install-zlib-support – RanRag Jan 31 '12 at 01:58
  • Did you self-compile PIL? Or is PIL managed by `apt`? – sarnold Jan 31 '12 at 01:59
  • @sarnold: I believe PIL was installed by ReportLab. In any event, I did not build it I got it either as part of something else or with easy_install on Windows and apt-get on Ubuntu. – Larry Lustig Jan 31 '12 at 02:09
  • @RanRag: I was able to use those instructions to solve the problem (had to reinstall PIL after sym-linking the so for zlib. Please post as an answer so you can claim your points (and the next person with the same problem can find the answer). – Larry Lustig Jan 31 '12 at 02:36
  • @LarryLustig : I posted the answer if you feel anything missing or wrong please edit it. – RanRag Jan 31 '12 at 02:47

1 Answers1

12

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

RanRag
  • 48,359
  • 38
  • 114
  • 167
  • I have the same problem but I'm missing libz.so. Running linux mint and arch gives me i686 – rtn Feb 20 '12 at 16:54