4

I'm trying to generate PDF's with Images.

im = ImageReader('00001.png')
c = canvas.Canvas('networkanalyze.pdf', pagesize=A4)
c.drawImage(im, 10, 10, mask='auto')
c.showPage()
c.save()

Traceback:

Traceback (most recent call last):
  File "pdf.py", line 9, in <module>
    c.drawImage(im, 10, 10, mask='auto')
  File "/usr/lib/python2.6/site-packages/reportlab-2.7-py2.6-linux-x86_64.egg/reportlab/pdfgen/canvas.py", line 909, in drawImage
    rawdata = image.getRGBData()
  File "/usr/lib/python2.6/site-packages/reportlab-2.7-py2.6-linux-x86_64.egg/reportlab/lib/utils.py", line 656, in getRGBData
    annotateException('\nidentity=%s'%self.identity())
  File "/usr/lib/python2.6/site-packages/reportlab-2.7-py2.6-linux-x86_64.egg/reportlab/lib/utils.py", line 653, in getRGBData
    self._data = im.tostring()
  File "/usr/lib/python2.6/site-packages/Pillow-3.2.0-py2.6-linux-x86_64.egg/PIL/Image.py", line 699, in tostring
    "Please call tobytes() instead.")
Exception: tostring() has been removed. Please call tobytes() instead.

2nd Approach:

def generate_pdf(c):
    """
    letter :- (612.0, 792.0)
    """
    im = Image.open("00001.png")   
    c.drawInlineImage(im, 256, 720, width=100, height=60)

c = canvas.Canvas("report_image.pdf", pagesize=letter)
generate_pdf(c)
c.save()

Traceback:

Traceback (most recent call last):
  File "pdf2.py", line 14, in <module>
    generate_pdf(c)
  File "pdf2.py", line 11, in generate_pdf
    c.drawInlineImage(im, 256, 720, width=100, height=60)
  File "/usr/lib/python2.6/site-packages/reportlab-2.7-py2.6-linux-x86_64.egg/reportlab/pdfgen/canvas.py", line 837, in drawInlineImage
    img_obj = PDFImage(image, x,y, width, height)
  File "/usr/lib/python2.6/site-packages/reportlab-2.7-py2.6-linux-x86_64.egg/reportlab/pdfgen/pdfimages.py", line 42, in __init__
    self.getImageData()
  File "/usr/lib/python2.6/site-packages/reportlab-2.7-py2.6-linux-x86_64.egg/reportlab/pdfgen/pdfimages.py", line 156, in getImageData
    imagedata, imgwidth, imgheight = self.PIL_imagedata()
  File "/usr/lib/python2.6/site-packages/reportlab-2.7-py2.6-linux-x86_64.egg/reportlab/pdfgen/pdfimages.py", line 117, in PIL_imagedata
    raw = myimage.tostring()
  File "/usr/lib/python2.6/site-packages/Pillow-3.2.0-py2.6-linux-x86_64.egg/PIL/Image.py", line 699, in tostring
    "Please call tobytes() instead.")
Exception: tostring() has been removed. Please call tobytes() instead.

So it seems to not be code related.

I am running python on a server:

Python 2.6.6 (r266:84292, Nov 21 2013, 10:50:32) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2

Version of Pillow: Pillow-3.2.0-py2.6-linux-x86_64.egg

Version of reportlab: reportlab-2.7-py2.6-linux-x86_64.egg

I searched for this particular error without success, what can I do to solve this?

Roy Holzem
  • 860
  • 13
  • 25

3 Answers3

5

From the traceback, you can see that reportlab is calling the tostring() method, which was deprecated by that commit in Pillow.

Thus, your code would probably work if you downgrade Pillow to version 3.1.

However, your version of reportlab is quite outdated as you have the version 2.7 and the version 3.3 is released. I didn't try it but I guess they fixed the issue and at least, it's worth to try!

The latest version of reportlab is not compatible with Python 2.6 but you should probably upgrade at least to Python 2.7, or even to Python 3 :)

filaton
  • 2,257
  • 17
  • 27
  • Downgraded to 3.1 same error. I would upgrade to python 2.7 but I fear that I have to reinstall all my python modules on 2.6 like bs4 etc. I cant use pip because firewall on virtualenv. – Roy Holzem Apr 13 '16 at 09:49
  • 2
    About the version, sorry for the mistake but the 2.9 instead of 3.1 should be good (you should have a warning but not an exception). About upgrading Python, you're right: if you can't access pip, you'll have to reinstall all modules. I'd say it's still worth to spend time upgrading though, because Python 2.6 gets really old ;) – filaton Apr 13 '16 at 11:05
  • This fixed the problem. Thank you so much. I will upgrade my env but I have a presentation on friday and can't risk my application to fail then. Any way to hide the warning? – Roy Holzem Apr 13 '16 at 12:45
  • 1
    @Rizzit To temporarily hack hiding the warning you could edit /usr/lib/python2.6/site-packages/Pillow-3.2.0-py2.6-linux-x86_64.egg/PIL/Image.py and find `def tostring(self, *args, **kw):` `raise Exception("tostring() has been removed. " +` `"Please use tobytes() instead.")` and replace it with `def tostring(self):` `return self.tobytes()` – Hugo Apr 13 '16 at 13:10
  • 1
    Another way would be to downgrade again to the version when it wasn't deprecated but I don't know which one would work and you would probably lose features. But I would go with Hugo's method in your case :) – filaton Apr 13 '16 at 13:34
  • I used Hugo's hack and it works like a charm, until I have clear way with support from my sysadmin to upgrade my python and all modules. Thank you all for your support, this is why I love stackoverflow :) – Roy Holzem Apr 14 '16 at 14:08
5

I have been able to go on by monkey-patching Pillow. Don't do this at home:

from PIL import Image
# Bad hack
Image.Image.tostring = Image.Image.tobytes
rapto
  • 405
  • 3
  • 15
0

Downgrading to pillow 2.9.0 worked for me on an ubuntu 14.04 using Pip. Just type:

 pip uninstall pillow
 pip install pillow==2.9.0

Hope it works for you

simo23
  • 506
  • 4
  • 12