[Image-SIG] Place image into MySQL db.

S. D. Rose s_david_rose at hotmail.com
Mon Dec 19 17:00:14 CET 2005

I think I should have been a little more specific.  I have a process that 
takes a bunch of photos and stores them 'as taken' in .JPG format.  I read 
them from the database like this:

>>> cur.execute("select * from photosTMP")
>>> result = cur.fetch()
>>> for each in result:
             recno = each[0]
             img[counter] = each[1].tostring()
            counter += 1

.. do something ..

>>> for photoNum in range(0,4):
             cur.execute("insert into photo(check_transactionKEY, photo) 
values(%s, %s)", (photoNum, img[photoNum] )

Now, I want to do something to them.  Resize them to 320x240, make them 
grayscale.  Possibly a rotation.  After doing what I want to do, I want to 
insert them into another table  for permanent storage.  However, at this 
point, they seem to have lost their .JPG formatting.  That's what I wrote 
about before.  I _don't_ want to save them to a file.  Rather I want to 
insert the image into the database.  If I insert the img object, isn't that 
the PIL object, rather than just the image?  I'd really like to be able to 
put the image-only in.

I guess I could img.save(file, 'jpg') and then read the file and then insert 
it, but that seems to be a particularly in-elegant way to do it.

If someone could assist, I'd really appreciate it.


"Fredrik Lundh" <fredrik at pythonware.com> wrote in message 
news:do35pg$38q$1 at sea.gmane.org...
> "S. D. Rose" wrote:
>> I have a question about PIL.
>> I get a 50k photo from a MySQL table, convert it to grey-scale, do some
>> rotations, etc.  Then I want to put it back.  It seems that after I do 
>> the
>> greyscale, it converts from 'JPEG' to 'RAW'.  Can anyone tell me how I
>> convert the image back to 'JPEG' and then insert the photo into the MySQL
>> database? I've tried .tostring('jpeg') but that told me it couldn't load 
>> the
>> jpeg encoder.
> "tostring" converts the pixel contents to a string buffer, much like 
> array.tostring
> and similar methods in the Python library.
> if you want to save the image to an external file format, use the "save" 
> method.
> to save to a string, use a StringIO or cStringIO stream as the target:
>    out = cStringIO.StringIO()
>    im.save(out, "jpeg")
>    data = out.getvalue()
> </F>
> _______________________________________________
> Image-SIG maillist  -  Image-SIG at python.org
> http://mail.python.org/mailman/listinfo/image-sig

More information about the Image-SIG mailing list