CGI -- generating images -- \x0A always added!

M

M-a-S

Hi All,

I'm trying to use Python for generating (or copying) images.
The environment is Python 2.3/Apache 2.0.43/Windows XP.
I couldn't make Apache use mod_python (anybody can help?
Apache complains "The requested operation has failed"
if I add LoadModule python_module modules/mod_python.so)
so I use the CGI mechanism. This is the sample:

#!C:\Apps\Python\python.exe -u

img = open( 'a.gif', 'rb' ) # OK, don't make the image, just use the file
if img:
data = img.read()
img.close()

print "Content-type: image/gif"
print

print data,

# EOF

Everything's good, and '-u' helps not to add '\x0D' for each '\x0A'
inside the data. BUT it seems that the last print ALWAYS adds LF
'\x0A' in spite of the final comma. How can I fix that? Thank you.

M-a-S
 
B

Bengt Richter

Hi All,

I'm trying to use Python for generating (or copying) images.
The environment is Python 2.3/Apache 2.0.43/Windows XP.
I couldn't make Apache use mod_python (anybody can help?
Apache complains "The requested operation has failed"
if I add LoadModule python_module modules/mod_python.so)
so I use the CGI mechanism. This is the sample:
I would try switching to using sys.stdout.write
#!C:\Apps\Python\python.exe -u
import sys
img = open( 'a.gif', 'rb' ) # OK, don't make the image, just use the file
if img:
data = img.read()
img.close()

print "Content-type: image/gif"
print
The above two might work ok, but might as well use
sys.stdout.write consistently instead, e.g.,

# untested ...
sys.stdout.write('Content-type: image/gif\n\n') # should be the same
print data,
sys.stdout.write(data)
sys.stdout.close()
# EOF

Everything's good, and '-u' helps not to add '\x0D' for each '\x0A'
inside the data. BUT it seems that the last print ALWAYS adds LF
'\x0A' in spite of the final comma. How can I fix that? Thank you.
HTH. It should be easy to try. Let us know.

Regards,
Bengt Richter
 
K

Kevin

This works for me (save to a StringIO object first):

###############################

#!/usr/bin/python -u

# imports

import Image, sys, string, os, cgi, StringIO

# blah blah blah... prepare the image from the query string...

if os.path.isfile(filename):

try:

image = Image.open(filename)

# save the image to stdout

sfp = StringIO.StringIO()

image.save(sfp,'jpeg')

except Exception, errorMsg:

print "Content-Type: text/html\n\n"

print "Error occurred while processing Image (%s)<p>\n\n" %filename

print "%s<p>\n%s" %(Exception, errorMsg)

sys.exit(-1)

else:

print "Content-Type: text/html\n\n"

print "Error: requested image file does not exist: %s" %filename

sys.exit(-1)

# if you got here, everything went ok, and the image is in spf, just send it
home

try:

print 'Content-Type: image/jpeg\n' # Python adds the extra line feed to
separate the header

sys.stdout.write(sfp.getvalue())

sys.exit(0)

except Exception, errorMsg:

# something went wrong in writing to sys.stdout?

print "Content-Type: text/html\n\n"

print "Error Printing Image and Header to sys.stdout\n<p>\n%s<p>\n%s"
%(Exception, errorMsg)

sys.exit(-1)
 
K

Kevin

Actually the easy answer to your question is use:

sys.stdout.write(yourImageData)

instead of

print yourImageData


That won't print the newline at the end.

Kevin.
 
M

M-a-S

print "foo",;sys.stdout.softspace = 0

This doesn't work. \x0A is added anyways. And from what I've read it shoudn't work -- it influences only the blank BEFORE each print
that doesn't go after NL.
sys.stdout.write( "Content-type: image/gif\n\n" )
sys.stdout.write( data )
sys.stdout.close() # or flush()

Doesn't work at all. Server error. Premature end of script headers.
 
M

M-a-S

I'm very sorry. Of course it was my fault. I forgot import sys :) This code works. Thank you all!

sys.stdout.write( "Content-type: image/gif\n\n" )
sys.stdout.write( data )
sys.stdout.flush() # or close()
 
M

M-a-S

Thank you!
M-a-S

Bengt Richter said:
# untested ...
sys.stdout.write('Content-type: image/gif\n\n') # should be the same
HTH. It should be easy to try. Let us know.

Regards,
Bengt Richter
 
J

John Hazen

Before the program exits, you can set sys.stdout.softspace to 0, and that
will prevent the pending newline from printing.

In the interactive mode:foo>>> # no newline


But the way which is probably most appropriate for dealing with
binary data (it should also make the '-u' unneccessary) is:
foo>>> # no newline...

HTH-

John
(john AT <my domain (see from address)>)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top