[英]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)] on linux2
枕頭版:Pillow-3.2.0-py2.6-linux-x86_64.egg
reportlab的版本:reportlab-2.7-py2.6-linux-x86_64.egg
我沒有成功搜索這個特殊錯誤,我該怎么做才能解決這個問題?
我已經能夠通過猴子修補Pillow繼續。 不要在家里這樣做:
from PIL import Image
# Bad hack
Image.Image.tostring = Image.Image.tobytes
降級到枕頭2.9.0使用Pip在ubuntu 14.04上為我工作。 只需輸入:
pip uninstall pillow
pip install pillow==2.9.0
希望這對你有用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.