More & More Fun w/ Pics & MySQL

Lie Ryan lie.1296 at gmail.com
Sat Oct 17 14:08:05 EDT 2009


Carsten Haese wrote:
> Victor Subervi wrote:
>> [snip...]
>>
>> print 'Content-type: image/jpeg'
>> print 'Content-Encoding: base64'
>> print
>> print pic().encode('base64')
>> print '</body></html>'
>>
>> [snip...]
> 
> Why are you printing "</body></html>" at the end of a page that is
> supposed to be a base64-encoded JPEG file?

I'm testing my "psychic" mode today.

<psychic>
Your old script produces the line drawing image in 
http://allpointsmarinevi.com/stxresort/cart/getpic1.py; a JPEG image to 
be exact.

Now, you want to embed this image into a webpage.

You thought: "well I could simply put the code I used previously to the 
code that produced the HTML"

Let's say, you used something like this to produce the image:

###########################################
# in file getpic.py
# for simplicity, the image is pre-generated
def foo():
     img = open('myimg.jpg')
     print 'some header information'
     print img.read()
###########################################



While your HTML-producing code is something like this:

########################
# note that I avoided putting an exact working code
# so you don't start copy and pasting without
# digesting and understanding what you're doing

import getpic
print 'html header'
getpic.foo()
print 'html header end'
########################

You thought... this should work, I just called a code that previously works.

Well, if my psychic prediction capability doesn't have bugs in it, and 
the weather is fine today, and the moon is full, and the 42nd road is 
not jammed today; then, as Carsten Haese remarked before, you have no 
idea why getpic.py works but calling a function it doesn't.

First off; HTML doesn't have a way to "embed" image information[1]. What 
it can do, and what you should do is to "link" the image (unless you 
want to invite neverending troubles including poor browser support and a 
standard that practically nobody uses).

First and foremost though, please understand how HTML works; 
particularly <img> tag.
This is how you "link" image inside a webpage:
<img src="relative/url/to/image.jpg" />

Since you don't actually use a static image but a script-generated 
image; as long as the server is configured to handle python script, you 
can use this as well:
<img src="relative/url/to/image.py" />

[1] actually, there is a standard for image embedding; but browser 
support is flaky, at best. Especially for IE. Hint: <img 
src="data:image/png;base64,BASE64ENCODEDIMAGESTRING" />

</psychic>



More information about the Python-list mailing list