简体   繁体   English

reportlab图像到PDF:“请致电tobytes()”

[英]reportlab Image to PDF: “please call tobytes()”

I'm trying to generate PDF's with Images. 我正在尝试使用图像生成PDF。

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:

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 Python 2.6.6(r266:84292,2013年11月21日,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 枕头版:Pillow-3.2.0-py2.6-linux-x86_64.egg

Version of reportlab: reportlab-2.7-py2.6-linux-x86_64.egg 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? 我没有成功搜索这个特殊错误,我该怎么做才能解决这个问题?

From the traceback, you can see that reportlab is calling the tostring() method, which was deprecated by that commit in Pillow. 从回溯中,您可以看到reportlab正在调用tostring()方法,该方法在Pillow中被该提交弃用。

Thus, your code would probably work if you downgrade Pillow to version 3.1. 因此,如果将Pillow降级到3.1版,您的代码可能会有效。

However, your version of reportlab is quite outdated as you have the version 2.7 and the version 3.3 is released. 但是,您的版本的reportlab已经过时,因为您拥有版本2.7并且版本3.3已发布。 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 :) reportlab的最新版本与Python 2.6不兼容,但你应该至少升级到Python 2.7,甚至升级到Python 3 :)

I have been able to go on by monkey-patching Pillow. 我已经能够通过猴子修补Pillow继续。 Don't do this at home: 不要在家里这样做:

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

Downgrading to pillow 2.9.0 worked for me on an ubuntu 14.04 using Pip. 降级到枕头2.9.0使用Pip在ubuntu 14.04上为我工作。 Just type: 只需输入:

 pip uninstall pillow
 pip install pillow==2.9.0

Hope it works for you 希望这对你有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM