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

Shonn Gilson shonn at
Wed Mar 7 06:54:24 CET 2001

It looks like I've figured out what part of the problem is but I don't have
a solution (well I do, but it's really ugly). Oh, and I'm new to python
coming from a VB background.
Okay, Python is processing the strings right (the tutorial didn't specify
that '\377' is octal not decimal.). The problem seems to be that when Python
passes strings through COM that they get translated to Unicode. This happens
for things like Response.BinaryWrite along with things like recordsets
(objRS = Server.CreateObject("ADODB.Recordset")).  The result is that the
beginning of my jpg file looks like
'\377\000\330\000\377\000\340\000\000\000\020\000J\000F\000I\000F' instead
of '\377\330\377\340\000\020JFIF'.

Does anyone know how to handle this?


"Shonn Gilson" <shonn at> wrote in message
news:ta0amgebq0sn0b at
> 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
> 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 =
> im.thumbnail((128, 128))
> bstrOut = StringIO.StringIO()
>, "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
> 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