[reportlab-users] load Image() from embedded python

849 views
Skip to first unread message

Michael Hipp

unread,
Jan 28, 2014, 7:50:59 PM1/28/14
to reportlab-users
In the reportlab.platypus Image flowable, is it possible to load the image from
an image embedded as binary in the python code? The only api for Image seems to
depend on a filename.

I'd prefer not to have to lug around things like logos and icons with my program.

Thanks,
Michael
_______________________________________________
reportlab-users mailing list
reportl...@lists2.reportlab.com
http://two.pairlist.net/mailman/listinfo/reportlab-users

Andy Robinson

unread,
Jan 29, 2014, 2:47:40 AM1/29/14
to reportlab-users
I think that everywhere we accept a filename, we also accept a
file-like object. So you could store it as some kind of escaped
binary string in your source code, then wrap it in a StringIO and pass
that to the platypus Image.

I'm not in a programming environment now but if this is not clear we
can post a code snippet later in the day...

- Andy
--
Andy Robinson
Managing Director
ReportLab Europe Ltd.
Thornton House, Thornton Road, Wimbledon, London SW19 4NG, UK
Tel +44-20-8405-6420

Michael Hipp

unread,
Feb 1, 2014, 12:27:39 PM2/1/14
to reportl...@lists2.reportlab.com
Thanks Andy.

It does indeed work. For anyone else that might try this, here's what I did...

Use bin2py to convert the jpg image file to python:
https://github.com/MarioVilas/shellcode_tools/blob/master/bin2py.py

> bin2py logo.jpg -o logo.py

Then put in the program something like this:

import logo
import StringIO
from reportlab.platypus import Image

img = Image(StringIO(logo.logo), width=48, height=48)

Works swimmingly and now I don't have to package the logo.jpg file as a data
file with the program.

Note that I was not able to get ReportLab Image() to work with the output from
wxPython's img2py utility as I had originally intended, hence the use of bin2py.

Michael

Michael Hipp

unread,
Feb 8, 2014, 12:54:32 PM2/8/14
to reportlab-users
As noted in my previous reply I was able to make this work great with the
flowable Image().

Now I'm doing something like this, and getting the exception below. Any
suggestions?

canvas.drawImage(StringIO('embedded stuff'), ...)

I would be really helpful to be able to embed these things.

I'm using reportlab 3.0a1 BTW.

Thanks,
Michael

File
"c:\dev\virtenvs\oldworldblack\lib\site-packages\reportlab\pdfgen\canvas.py",
line 920, in drawImage
imgObj = pdfdoc.PDFImageXObject(name, image, mask=mask)
File
"c:\dev\virtenvs\oldworldblack\lib\site-packages\reportlab\pdfbase\pdfdoc.py",
line 2086, in __init__
ext = os.path.splitext(source)[1].lower()
File "c:\dev\virtenvs\oldworldblack\lib\ntpath.py", line 190, in splitext
return genericpath._splitext(p, sep, altsep, extsep)
File "c:\dev\virtenvs\oldworldblack\lib\genericpath.py", line 91, in _splitext
sepIndex = p.rfind(sep)
AttributeError: StringIO instance has no attribute 'rfind'



On 1/29/2014 1:47 AM, Andy Robinson wrote:

Robin Becker

unread,
Feb 9, 2014, 5:12:45 AM2/9/14
to reportlab-users
Not sure if this is with python 2.7 or 3.3, but there are differences
in the way StringIO works in the two dialects. In practice I think for
any kind of image object we ought to be reading in binary so StringIO
is off the table. Can you try passing an ImageReader instance to the
canvas. That would look like


from reportlab.lib.utils import ImageReader, getBytesIO

.......


canvas.drawImage(ImageReader(getBytesIO('embedded stuff')))



......

ImageReader is acceptable to drawImage & a bytes io object is
acceptable to ImageReader (I think).
--
Robin Becker

Michael Hipp

unread,
Feb 9, 2014, 10:10:40 AM2/9/14
to reportlab-users
I'm using Python 2.7.6 on windows. I tried both cStringIO and StringIO
with no luck. But the combo of ImageReader and getBytesIO works great.
Thank you.

Michael

Robin Becker

unread,
Feb 9, 2014, 1:06:57 PM2/9/14
to reportlab-users
Yes I thing you can pass either an ImageReader or a filename, but not
a file like object into drawImage.
--
Robin Becker

Andy Robinson

unread,
Feb 9, 2014, 1:38:28 PM2/9/14
to reportlab-users
We'd better make sure we get this right in the docs...
--
Andy Robinson
Managing Director
ReportLab Europe Ltd.
Thornton House, Thornton Road, Wimbledon, London SW19 4NG, UK
Tel +44-20-8405-6420
Reply all
Reply to author
Forward
0 new messages