Using PIL with CGI scripts

Mark Nottingham mnot at pobox.com
Fri Jun 11 21:13:08 EDT 1999


Luis,

what you have to do is call the script twice. For instance, if you decide
that you need to display foo.jpg (which you've got in a pickle), you first
need to output html like this:

...
<IMG SRC="/path/to/this/script/scriptname?show=foo.jpg">
...
replacing /path/to/this/script/scriptname with the URL of the script
generating the request.

The browser will get this, and when it gets to the IMG tag, it will re-call
your script, this time with foo.jpg as an argument. Your script needs to be
able to take that request and return the proper HTTP headers, along with the
image data itself as the body. For instance (assuming that img is your
Image() instance):

tmp = StringIO()
img.save(tmp, 'JPEG')
tmp.seek(0)
image = tmp.read()

print '''\
Content-type: image/jpeg
Content-length: %s

%s''' % (len(image), image)


u
----- Original Message -----
From: Luis Roco Arriagada <luisroco at hotmail.com>
To: <mnot at pobox.com>; <python-list at cwi.nl>
Sent: Saturday, June 12, 1999 8:31
Subject: Re: Using PIL with CGI scripts


> Mark:
>
> Sorry for bothering you again, but I can't figure out how can I do this
> work.
>
> I can`t write the image to the disk because all of the images are inside a
> single file where they are compressed using pickle. What I'm trying to do
is
> to get some images from this file according to a word search perform by
the
> user of the web site. This part is already done, so the images are
extracted
> from the file but they are in a PIL format, ie they are an Image type, and
> for that i cannot display them in a web page using the HTML tag <IMG SRC =
>
> because this tag only accepts a file name as input.
>
> I try to use cStringIO to emulate a file, but I don't know how I must use
> this variable with the HTML tag.
>
> This part of the code is something like this:
> from cStringIO import StringIO
>
> print "Content-type: text/html"
> print
> print "<HEAD><TITLE>WEBIMAGE</TITLE></HEAD>"
> print "<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\" LINK=\"#0000FF\"
> VLINK=\"#000080\" ALINK=\"#FF0000\">"
> print "<center><H1>WEBIMAGE</h1>"
> print "<p>Version 0.01</p>"
> print "<hr>"
> .
> .
> .
> temp = StringIO()
> im.save(temp,"JPEG") #im is an Image variable
> temp.seek(0)
> print "<p><IMG SRC='%s'>"%temp
>
> print "</center>"
> print "</body>"
>
>
> Hope you can help me and thanks in advance
> Luis Roco
>
> >From: "Mark Nottingham" <mnot at pobox.com>
> >To: <luisroco at hotmail.com>,<python-list at cwi.nl>
> >Subject: Re: Using PIL with CGI scripts
> >Date: Wed, 9 Jun 1999 08:27:23 +1000
> >
> >See the StringIO module (actually, cStringIO).
> >
> >One thing to keep in mind - unless the image is *really* dynamic (i.e.,
it
> >changes EVERY time someone accesses it), you're much better off if you
> >write
> >it to disk.
> >
> >See genpix.py at http://www.mnot.net/python/  for an example of this
(it's
> >a
> >command line utility that HTMLifys a directory of images, but it does use
> >cStringIO in this manner).
> >
> >Cheers,
> >
> >
> >----- Original Message -----
> >From: <luisroco at hotmail.com>
> >To: <python-list at cwi.nl>
> >Sent: Wednesday, June 09, 1999 7:10
> >Subject: Using PIL with CGI scripts
> >
> >
> > > Hi all
> > >
> > > How can a CGI scripts outputs graphics images through Python code
using
> >the Python Imaging Library package?
> > >
> > > I just want to produce some images which has been processed using PIL
> >and
> >show them in a web site
> > >
> > >
> >
>
>
> ______________________________________________________
> Get Your Private, Free Email at http://www.hotmail.com
>





More information about the Python-list mailing list