[mod_python] Image handling handler

Sean Treadway seant at superchannel.org
Thu Sep 23 22:27:27 EDT 2004


I have written a similar handler, and found that some PIL image formats
needed as seekable file.  It was a long time ago, so PIL may have lifted
that requirement.

-Sean 

> -----Original Message-----
> From: mod_python-bounces at modpython.org [mailto:mod_python-
> bounces at modpython.org] On Behalf Of Damjan
> Sent: Thursday, September 23, 2004 7:31 PM
> To: mod_python user mailing list
> Subject: Re: [mod_python] Image handling handler
> 
> >>> I'm probably missing something obvious here, but in the 'handle_thumb'
> >>> and 'handle_size' class methods if the image is not cached (or is
> older)
> >>> where is the method sending the image file back to the client?
> >>> It appears only to resize and cache the image not return it.
> >>
> >> img.save(self.req, format=img.format)
> >>
> >> I'm "saving" the image to the req object. mod_python's request object
> is
> >> fine at emulating a file object.
> >
> > I'm probably having one of those dumb moments (It happens often!) but I
> > just don't get this.
> 
> :)
> 
> > How does the request object know its supposed to be a file object and
> > relay the image to the browser.
> 
> The request object is "file object" compatible, that means that any
> python function that works with file objects can work with the request
> object too. Like this:
> 
> def fill_file_with_zeros(something, howmany):
> 	buff = '\x0' * howmany
> 	something.write(buff)
> 	return
> 
> Now I can call that function like this:
> 
> fp = file('/tmp/zeroed')
> fill_file_with_zeros(fp, 100)
> 
> or in mod_pytnon like this:
> 
> fill_file_with_zeros(req, 100)
> 
> The function doesn't care if "something" is really a file. Its enough if
> "something" has the write method.
> So in the second case, the function fill_file_with_zeros calls "req"s
> write method... what happens is that the client receives a file filled
> with
> NULLs (well, that's not very usefull ;) ).
> 
> > When/where does the req.write get invoked to send the image?
> 
> Inside img.save(...), img.save is just like the above
> fill_file_with_zeros,
> it doesn't care if you give it a real file, or a file like object (like
> req)... as long as it supports the write method.
> 
> Btw, I could've done it like this:
> 
> 	buff = StringIO.StringIO()
> 	img.save(buff,...)
> 	req.write(buff.getvalue())
> 
> But that would only use twice the memory for no reason.
> 
> --
> damjan | ??????
> This is my jabber ID --> damjan at bagra.net.mk <-- not my mail address!!!
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python




More information about the Mod_python mailing list