[mod_python] Dynamic <IMG> with PIL

Robert Norman tfcrobert at gmail.com
Fri May 18 22:10:42 EDT 2007

Hi Colin,

That did it! I hadn't understood the underlying process.
So my handler gets called once for the main page and again with the name of
the image file contained in the IMG tag. The handler just calls the image
generator and merges it into the page. Worked great!  I already had the
image to StringIO working, so it was simply a matter of staging the

Mod-python is... cool.


On 5/18/07, Colin Bean < ccbean at gmail.com> wrote:
> Hi Robert,
> An image tag in your document won't contain any actual image data, it
> simply provides a URL which the browser uses to fetch that image data.
> So in order to do what you want to do, you'll need at least two
> separate handlers: one to generate the initial page and one to
> generate some dynamic image content.
> To explain a little better: after a browser initially loads an HTML
> page, it will find the SRC attribute for each IMG tag and then perform
> a *separate* HTTP request to that URL, and expect some understandable
> image data back.  So you will need to generate the image content
> separately from the page, and vice versa.
> The suggestion that you posted is probably pretty close to what you
> will want to do:  you'll have a hander which generates your image in
> PIL, sets the content type, and writes the properly encoded image data
> (and nothing else) to the request.  I don't use publisher, but I'm
> pretty sure this will NOT work with publisher, you will probably have
> to write a small custom handler to do this.  Might look something like
> this pseudocode (note I haven't tested this in any way):
> from cStringIO import StringIO  #Way to hold the encoded image data, for
> now
> def handler(req):
>     #Grab whatever parameters you need...
>     #Generate a PIL image, let's call it img....
>     s = StringIO()
>     img.save(s, 'jpeg')  #We have to encode this as a jpeg or similar
> format,
>                                 #if we send the raw PIL data the
> client won't understand it
>     s.seek(0)
>     req.content_type = 'image/jpeg'  #Or whatever matches what we save
> it as above
>     #Would be nice to set the content length, too...
>     req.write(s.getvalue()) #Send the encoded image data as our body
>     return apache.OK
> A good place to start might be setting this up so you can enter a url
> like /myimagehanlder?id=foo and have it display just the image in your
> browser.  Then integrating it into your page may make more sense.  I
> would also recommend implementing some kind of caching mechanism, as
> the above code will be a lot of work for your server.
> HTH,
> Colin
> On 5/18/07, Robert Norman <tfcrobert at gmail.com> wrote:
> > I'm trying to create graphs on the fly with mod-python.  Using PIL I can
> get
> > a "jpeg" image as a string. Is there some way to wrap this information
> in an
> > IMG tag?  I gather that  src parameter in IMG is required so anythink
> like:
> >
> > req.write("<IMG %s>"  % myString)
> >
> > is not going to work.
> >
> > One suggestion was to:
> >
> >  <img src=" get-graph.py?graphId=xyz" />
> >
> > where:
> >
> > "get-graph.py is a python script. it sets the header to "image/jpeg" or
> > whatever format you want to use than you can use some python libraries
> to
> > create the image. Instead of saving the image to a file you just write
> it to
> > stdout."
> >
> > I don't understand the suggestion especially in the context of
> mod-python
> > since we want to use req to write the page.
> >
> > What is a reasonable method to generate dynamic image graphics?
> >
> > Robert
> >
> >
> > _______________________________________________
> > Mod_python mailing list
> > Mod_python at modpython.org
> > http://mailman.modpython.org/mailman/listinfo/mod_python
> >
> >
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20070518/4e58deac/attachment.html

More information about the Mod_python mailing list