<html><div style='background-color:'><P>Thanks for the reply</P>
<P>I read first the one Danny sent me.</P>
<P>I'll try the code and let you know how it went</P>
<P>About the overwhelming of the machine, is a Dedicated Server so I don't think that would happen. The person in charge of the project told me that he needed a daemon to run under Linux to store in the database so he could take out the binary string into php and show it in a web site, so I think that's why he wants to store it in a database. Sorry I didn't comment this before it's just that I found out this yesterday :P</P>
<P>Thanks in advanced</P>
<P>Alberto </P>>From: Matthew Dixon Cowles <matt@mondoinfo.com> >Reply-To: help@python.org >To: Alberto Troiano <albertito_g@hotmail.com> >CC: help@python.org >Subject: Re: [Python-Help] Support with Image >Date: Sat, 9 Apr 2005 13:52:52 -0500 (CDT) > >Dear Alberto, > > > but now I don't know how to retrieve the image > > > outFile = StringIO() > > im.save(outFile, "JPEG") > > > cursor.execute("insert into image values('',%s)",outfile) > >It would surprise me very much if that worked correctly. Have you >checked to see what's stored in your database after you've done that? > >What Danny said was: > > Here, we use StringIO to use an in-memory file-like object: > > http://www.python.org/doc/lib/module-StringIO.html > > and we can use getvalue() to get at the binary bytes that you'll
> probably want to store into your database. > >Depending on just how you're getting the data for your images, you >may want to go through a StringIO object or you may be able to just >work with the bytes that are in the file. > >But it's pretty common that it's not a very good idea to store image >data in a database. If your images are pretty small and your database >has a "BLOB" (binary large object) type, you may have some luck. If >either of those isn't the case, the overhead of getting your images >into and out of the database may overwhelm your machine. You might >want to consider storing your image files semi-randomly in a >directory hierarchy and recording in your database only the path to >the file. If you have only a few hundred or a few thousand images, a >single directory may be sufficient. If you have many more of them >than
that, you may want to split them up according to some rule that >results in approximately equal numbers going in different directories. > > > cursor.execute("select * from image") > > res=cursor.fetchall() > > > > Now I have the binary string in res[1] but when I try to open it it > > gives me an error > >Here's where you may want a StringIO object. If whatever you're >giving the image to expects it in a file rather than a string, you >could do somwthing like: > > >>> s=StringIO.StringIO() > >>> s.write(res[1]) > >>> s.seek(0) > >and then give the variable s to whatever wants to read the file. > >Regards, >Matt > </div></html>