images on the web
mnordhoff at mattnordhoff.com
Fri Jun 20 08:41:14 CEST 2008
Matt Nordhoff wrote:
> chris wrote:
>> I'm creating a data plot and need to display the image to a web page.
>> What's the best way of doing this without having to save the image to
>> disk? I already have a mod_python script that outputs the data in
>> tabular format, but I haven't been able to find anything on adding a
>> generated image.
> You could use data: URIs .
> For example, a 43-byte single pixel GIF becomes this URI:
> They don't have universal browser support, but that might not be a
> problem in this case.
> As for generating them with Python, I'm not sure... I just used Hixie's
> data: URI kitchen  for the above example.
>  <http://tools.ietf.org/html/rfc2397>
>  <http://software.hixie.ch/utilities/cgi/data/data>
Oh.. As <http://bitworking.org/news/Sparklines_in_data_URIs_in_Python>
shows, the reason I couldn't find a data: URI Python library is because
they're utterly trivial to generate:
raw_data = create_gif()
uri = 'data:image/gif;base64,' + urllib.quote(base64.b64encode(raw_data))
(And it's even simpler if you leave out the base64-encoding.)
More information about the Python-list