[reportlab-users] Image in a Python page

David Frank david83 at sfsu.edu
Tue Jun 5 12:44:45 EDT 2007


Hi Tim and Andy,

Thank you very much for your replies. I understand that this was not a
reportlab question but you guys still helped me figure out a solution for
my problem.

Andy's concept worked. I made an 'images' folder under htdocs and moved
the image (created by reportlab) to that folder using the shutil.move
command in Python. Then I called the image using "../images/Newbars.gif"
and it worked like a charm.

Tim, I still dont get this Content-type thing. I tried what you advised,
but Python still ends up printing the line, as is, on the page. So, I put
'Content-type: image/gif\n' before 'Content-type: text/html\n' and the
page gives an error. I guess it's something to do with Apache and its
configuration. I think I should start studying it more deeply.

But, anyways, the problem was solved. Thank you very very much for all
your efforts.

Regards,

David Frank





>> Hi All,

>>

>> This might not probably be the right place to post this question. But,

>> I was wondering if this problem was maybe because of ReportLab. I am

>> creating an image dynamically and I am using python in cgi to display

>> that image. I have Windows and I am running Apache 2.2 with Python 2.4

>> and Reportlab 2.1. This is my code for the python file, newbars.py and

>> for the image Newbars.gif. The major parts are in normal font and the

>> rest are in italics.

>>

>> #!C:/Python24/python

>> print "Content-type: text/html\n\n"

>> ...

>> renderPM.drawToFile(drawing, "Newbars.gif", "GIF")

>> print "Content-type: image/gif"

>> print 'Image Created'

>> print '<html><body><img src = "Newbars.gif"></img></body></html>'


cgi-bin is often a special directory for executables; the server may
be trying and failing to 'run the image' like another script.

Try making a new direcory under htdocs called 'images'. Try saving it
as '../images/newbars.gif'' (i.e. in htdocs, at the top) and refer to
it explicitly as '/images/newbars.gif' in your HTML. Then test it's
there with the browser manually.
there is nothing magic about 'images' as a directory name, but there
IS something magic about 'cgi-bin'.

- Andy

Andy gave you some good advice on the "special" nature of the cgi-bin
directory, but there was something else here I wanted to point out.

Notice that you have a "Content-Type" header at the beginning with a
blank line. After that, you can't issue any more headers. Everything
else is just content. So, the "image/gif" line will be seen as just
another line of HTML. It IS possible to have a CGI script return a GIF
image, by changing the very first line to "Content-Type: image/gif\n"
and then printing the GIF data as a unit.

Also note that you don't want TWO \n characters in that first line.
Remember that there is a newline at the end of every print statement.
Thus, you are getting TWO blank lines after the header. That's not a
problem for text/html, but it will fail for image/gif.

--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20070605/3a150d07/attachment.htm>


More information about the reportlab-users mailing list