[reportlab-users] drawImage function with file pointer

Robin Becker robin at reportlab.com
Thu Feb 2 05:13:34 EST 2006


John Keubler wrote:
> The following code does not work with a file pointer however if I hard code the file name into the function it does work.  Can anybody help me?  I need it to work with a file pointer because I am going to use the urlopen function and use it as a memory stream object.  I don't want to save files to my hard drive.  I want to do it all in memory.
>    
>   # this does not work
>   f = file("c:/tmpf.img", "rb")
>   a = c.drawImage(f, 3*inch, 3*inch, width=1*inch, height=1*inch, mask=None)
> print a
>    
>     # this does work
>   a = c.drawImage("c:/tmpf.img", 3*inch, 3*inch, width=1*inch, height=1*inch, mask=None)
> print a
......

You can use an ImageReader instance instead of a literal name. The ImageReader 
__init__ accepts a file name or URL or a StringIO or PIL Image instance as its 
first argument.

eg

from reportlab.lib.utils import ImageReader
im=ImageReader('http://www.reportlab.com/rsrc/encryption.gif')
c.drawImage(im)

etc
-- 
Robin Becker


More information about the reportlab-users mailing list