[reportlab-users] PIL frombuffer question

Paul Guse paul at mustagh.com
Thu Mar 8 17:53:07 EST 2007

Well I partially answered my own question. I can get a pixel buffer to write to the canvas as an image with the following code:

mode = 'RGB'
img = PIL.Image.frombuffer (mode, (100,100), b, "raw", mode, 0, 1)
img.fp = ""

i = ImageReader (img)
c.drawImage (i, 100, 100, 100, 100)

If I don't set img.fp I get an attribute error. This draws the image to the pdf correctly.
Anyways, this seems like a bit of a hack, and I'm sure there's a better way to do it. Also, it would be great to be able to create a platypus flowable out of a pixel buffer.
Does anyone have any tips for doing this?


----- Original Message -----
From: Paul Guse
To: reportlab-users at reportlab.com
Sent: Thursday, March 08, 2007 11:14 AM
Subject: [reportlab-users] PIL frombuffer question

Hello. I'm trying to write raw pixel data to a pdf using reportlab.
I've attempted to do this by creating a PIL image using frombuffer, create an ImageReader object from that PIL image, then draw this on the canvas. Here's my test code:

data = ''.join([chr(x%255) for x in range(30000)])
im = PIL.Image.frombuffer ('RGB', (100,100), data, "raw", 'RGB', 0, 1)
ir = ImageReader (im)

c = Canvas("test.pdf", pagesize=letter)
c.drawImage (ir, 100, 100, 100, 100)
c.showPage ()
c.save ()

However, I get the following error:
Traceback (most recent call last):
File "imagetest.py", line 3, in ?
ir = ImageReader (im)
File "reportlab\lib\utils.py", line 518, in __init__
File "C:\Python24\Lib\site-packages\PIL\Image.py", line 493, in __getattr__
raise AttributeError(name)
AttributeError: fp

So there seems to be problem with there not being a file? Any thoughts on what I'm doing wrong? Or a better way to accomplish this?


reportlab-users mailing list
reportlab-users at reportlab.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://two.pairlist.net/pipermail/reportlab-users/attachments/20070308/aefbfcfe/attachment.html

More information about the reportlab-users mailing list