reportlab 图片转 PDF:"please call tobytes()"

reportlab Image to PDF: "please call tobytes()"

我正在尝试生成带有图像的 PDF。

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

回溯:

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.

第二种方法:

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 (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.

所以好像和代码无关

我在 运行 python 服务器上:

Python 2.6.6(r266:84292,2013 年 11 月 21 日,10:50:32) [GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] linux2

枕头版本:Pillow-3.2.0-py2.6-linux-x86_64.egg

reportlab 版本:reportlab-2.7-py2.6-linux-x86_64.egg

我搜索了这个特定的错误但没有成功, 我该怎么做才能解决这个问题?

从回溯中,您可以看到 reportlab 正在调用 tostring() 方法,该方法已被 Pillow 中的 that commit 弃用。

因此,如果您将 Pillow 降级到版本 3.1,您的代码可能会正常工作。

但是,您的 reportlab 版本已经过时,因为您的版本是 2.7 并且 version 3.3 已发布。我没有尝试过,但我想他们解决了这个问题,至少值得一试!

reportlab 的最新版本与 Python 2.6 不兼容,但您应该至少升级到 Python 2.7,甚至升级到 Python 3 :)

我已经能够通过猴子修补枕头继续下去了。不要在家里这样做:

from PIL import Image
# Bad hack
Image.Image.tostring = Image.Image.tobytes

使用 Pip 在 ubuntu 14.04 上降级到 pillow 2.9.0 对我有用。只需输入:

 pip uninstall pillow
 pip install pillow==2.9.0

希望对你有用