[Image-SIG] Place image into MySQL db.

S. D. Rose s_david_rose at hotmail.com
Fri Dec 16 22:43:59 CET 2005


(Sorry - I accidentally posted to General Python...)

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.

My steps are here:

>>> import MySQLdb
>>> import array, cStringIO
>>> import Image, ImageOps

>>> con = MySQLdb(db='', host='', user='', passwd='')
>>> cur = con.cursor()
>>> cur.execute('SELECT photo FROM photos')
>>> result = cur.fetchone()
>>> photo = result[0].tostring()

>>> colorImg = Image.open(cStringIO.StringIO( photo))
>>> colorImg.show()   # Shows a color image on the Win32 screen

>>> img.format
'JPEG'

>>> bwImg =imageOps.grayscale(img)
>>> bwImg.format

>>> todb = bwImg.tostring(encoder_name='JPEG')
TRACEBACK ...
IOError: encoder JPEG not available.
>>>

Any help telling me how to take the bwImg image (not the PIL object) and
placing it back into a MySQL database would be highly appreciated!

Thanks!
Dave






More information about the Image-SIG mailing list