Binary blobs to jpeg

jayharvard1 at gmail.com jayharvard1 at gmail.com
Mon Jul 16 14:57:50 EDT 2007


Hello All.
I'm trying to create jpegs out of data that is stored in a binary blob
format in an RDBMS (MS Access). I've tried writing the jpeg directly:

while not rs.EOF:
     op = file(imgdir + "\\" + pic,"w")
     print >> op, rs.Fields[3].Value
     ##where rs.Fields[3].Value is the data in the blob from Access.

When I try to write out this file, the jpeg doesn't turn out.
Strangely, some of the colors partially render. You can see some
outlines and backgrounds, but the full picture doesn't show. Most
jpegs, however, show nothing (in an windows preview).

I then tried opening the image with PIL.Image, with something like
this:

cmd.CommandText = sql
rs=cmd.Execute()[0]
while not rs.EOF:
    im = Image.open(StringIO.StringIO(rs.Fields[3].Value))
    im.save("c:/projects/temp.jpg", 'JPEG', quality=90,progressive=1)



This is the error I get, I believe at the Image.open() call:
Traceback (most recent call last):
  File "<string>", line 97, in run
  File "C:\Python25\Lib\bdb.py", line 366, in run
    exec cmd in globals, locals
  File "C:\Projects\scripts\main.py", line 25, in <module>
    xdb.doImageTbl(db)
  File "C:\Projects\scripts\thunder.py", line 86, in doImageTbl
    im = Image.open(StringIO.StringIO(rs.Fields[3].Value))
  File "C:\Python25\lib\site-packages\PIL\Image.py", line 1916, in
open
    raise IOError("cannot identify image file")
IOError: cannot identify image file


For a little further information, I was able to accomplish this task
in C#/.Net. A direct write to a file stream produced the exact jpeg
image. So I know the data is not corrupt. I would like to avoid using
the C#/.Net solution if possible.

Any ideas what I'm doing wrong? Any help would be greatly appreciated.

BTW, I'm using Python 2.5 and PIL 1.1.6 on a WinXp box.

Thanks,
jeh




More information about the Python-list mailing list