Python, ASP, ADO, and Image string/binary problems

Shonn Gilson shonn at shonn.com
Fri Mar 2 18:17:35 EST 2001


Okay, here it goes.
I have a site in ASP where I need to upload an image (already working) and
then make a thumbnail of it. The image is stored in a MS SQL Server
database, I'm using IIS5 (Win2k), and the python is Active State's with all
the COM functionallity added.
Here's what I'm trying to do:

<script runat="server" language="Python">
import os, sys
import PIL.Image
import StringIO

strSQL = "select * from [FILE] where FILE_Guid =
'406D58D8C4EE4FA28DF1CFC988A49C11'"
objConn = Server.CreateObject("ADODB.Connection")
objRS = Server.CreateObject("ADODB.Recordset")
objConn.Open (connection_string)
objRS.Open (strSQL,objConn,3,1)
bstrIn = objRS("FILE_File").value # returns a buffer, not a string
im = PIL.Image.open(StringIO.StringIO(bstrIn))
im.thumbnail((128, 128))
bstrOut = StringIO.StringIO()
im.save(bstrOut, "JPEG",optimize=1,progressive=1)
Response.ContentType = 'text/html' #'image/jpeg'
Response.Write(" ")
Response.BinaryWrite(bstrOut.getvalue())
#Response.Write ('size')
#Response.Write (`im.size`)
objRS.Close()
objConn.Close()
</script>


If I do a Response.BinaryWrite(bstrIn) I get the image in the browser but if
I do Response.BinaryWrite(bstrOut.getvalue()) I get spaces between every
character (I've seen this with unicode problems). It's not just an
incompatibility with Response.BinaryWrite because I've written the results
back to the database with the same results. It seems like it's a problem
with how python deals with strings.

Anyone have any ideas?

Thanks in advance

Shonn





More information about the Python-list mailing list