469,935 Members | 1,695 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,935 developers. It's quick & easy.

images on the web

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.
Jun 27 '08 #1
5 1534
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 [1].

For example, a 43-byte single pixel GIF becomes this URI:

<data:image/gif;base64,R0lGODlhAQABAIAAAP%2F%2F%2F%2F%2F%2F%2F yH5BAEAAAEALAAAAAABAAEAAAICTAEAOw%3D%3D>

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 [2] for the above example.

[1] <http://tools.ietf.org/html/rfc2397>
[2] <http://software.hixie.ch/utilities/cgi/data/data>
--
Jun 27 '08 #2
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 [1].

For example, a 43-byte single pixel GIF becomes this URI:

<data:image/gif;base64,R0lGODlhAQABAIAAAP%2F%2F%2F%2F%2F%2F%2F yH5BAEAAAEALAAAAAABAAEAAAICTAEAOw%3D%3D>

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 [2] for the above example.

[1] <http://tools.ietf.org/html/rfc2397>
[2] <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:

import base64
import urllib

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.)
--
Jun 27 '08 #3
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.
Does your web application has session handling? Then you could save the
image in the session and server the separate HTTP request sent by the
browser.

Ciao, Michael.
Jun 27 '08 #4
Matt Nordhoff wrote:
Matt Nordhoff wrote:
>You could use data: URIs [1].

For example, a 43-byte single pixel GIF becomes this URI:

<data:image/gif;base64,R0lGODlhAQABAIAAAP%2F%2F%2F%2F%2F%2F%2F yH5BAEAAAEALAAAAAABAAEAAAICTAEAOw%3D%3D>

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 [2] for the above example.

[1] <http://tools.ietf.org/html/rfc2397>
[2] <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:

import base64
import urllib

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.)
The caveat with URL schema data: is that the amount of data to be
transferred is significantly higher than including HTML tag <img src="">
in your HTML source and let the browser fetch the raw binary image data
in a separate HTTP request (you also have to serve from your web
application).

Ciao, Michael.

Jun 27 '08 #5
On Jun 20, 1:52 am, Michael Ströder <mich...@stroeder.comwrote:
Matt Nordhoff wrote:
Matt Nordhoff wrote:
You could use data: URIs [1].
For example, a 43-byte single pixel GIF becomes this URI:
<data:image/gif;base64,R0lGODlhAQABAIAAAP%2F%2F%2F%2F%2F%2F%2F yH5BAEAAAEALAAAAAABAAEAAAICTAEAOw%3D%3D>
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 [2] for the above example.
[1] <http://tools.ietf.org/html/rfc2397>
[2] <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:
import base64
import urllib
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.)

The caveat with URL schema data: is that the amount of data to be
transferred is significantly higher than including HTML tag <img src="">
in your HTML source and let the browser fetch the raw binary image data
in a separate HTTP request (you also have to serve from your web
application).

Ciao, Michael.
This sounds like the way I want to go, it's just a matter of figuring
it out. Is it just a matter of putting a function call in an img tag?

I'll give the URI thing a try, too,
Jun 27 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Simon | last post: by
10 posts views Thread by Neo Geshel | last post: by
2 posts views Thread by mouseit101 | last post: by
reply views Thread by Frenchie | last post: by
1 post views Thread by Cerebral Believer | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.