我想使用drawImage方法将使用matplotlib生成的图形添加到reportlab画布中,而不必先将图形保存到硬盘驱动器中。
我的问题与: 是否有适用于ReportLab的matplotlib可流动?,很好地解决了。 但是,我不想使用DocTemplates,Stories,Flowables等。如上所述,我想使用drawImage将它放在画布中的某个位置。
我尝试使用以下方法将matplotlib图转换为PIL图像:
2) http://matplotlib.org/faq/howto_faq.html#matplotlib-in-a-web-application-server
例如,一些无法工作的代码是:
import Image
import matplotlib.pyplot as plt
import cStringIO
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch, cm
fig = plt.figure(figsize=(4, 3))
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
imgdata = cStringIO.StringIO()
fig.savefig(imgdata, format='png')
imgdata.seek(0) # rewind the data
im = Image.open(imgdata)
c = canvas.Canvas('test.pdf')
#c.drawImage(imgdata, cm, cm, inch, inch)
c.drawImage(im, cm, cm, inch, inch)
c.save()
试图画画 imgdata
导致错误:
AttributeError: 'cStringIO.StringO' object has no attribute 'rfind'
画画时 im
得到:
AttributeError: rfind
现在有人如何解决这个问题? 任何帮助将不胜感激。