[mod_python] Mixing content_type not displaying correct info

Sean Davis sdavis2 at mail.nih.gov
Tue Oct 9 08:05:00 EDT 2007

Jeff van Aswegen wrote:
> Hey guys
> I am trying to display 2 content types under 1 function/webpage, 
> 'image/jpeg' and 'text/html' but this is not working out. I am trying 
> to display a portion of an image and underneath that image have 3 text 
> fields that a capturer can enter some information on the image. This 
> is what I have so far.
> def show_image(req):
>     width, height = 1000, 1000
>     img = Image.open('/home/jeffmess/images/taco/F0476544.JPG')
>     img = img.resize((width,height), Image.ANTIALIAS)
>     box = (300,335,740,680)
>     region = img.crop(box)
>     img.paste(region, box)
>     s = StringIO()
>     region.save(s, 'jpeg')
>     file.write(s.getvalue())
>     file.close()
>     req.content_type = 'image/jpeg'
>     req.write(s.getvalue())
>     img.close()
>     return apache.OK
> Now on the same page if I include a random text field say,
>     req.content_type = "text/html"
>     req.write("""<INPUT TYPE='text' NAME='field0' ID='field0' 
> ONKEYDOWN='return tabOnEnter (this, event);' >""")
> Browsing to the page will show the text field but the image will all 
> be jargon. Can anyone perhaps offer some advice or point me in the 
> right direction? Thanks

You cannot mix content types.  What you should do is to save the image 
as a temporary file and make an IMG tag on the page that includes the 
text.  As an alternative to the temporary file, you can return the image 
from a separate function and call that function (as a URL, obviously) 
from the IMG tag.


More information about the Mod_python mailing list