Binary blobs to jpeg

jayharvard1 at jayharvard1 at
Mon Jul 16 20:57:50 CEST 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

cmd.CommandText = sql
while not rs.EOF:
    im =[3].Value))"c:/projects/temp.jpg", 'JPEG', quality=90,progressive=1)

This is the error I get, I believe at the call:
Traceback (most recent call last):
  File "<string>", line 97, in run
  File "C:\Python25\Lib\", line 366, in run
    exec cmd in globals, locals
  File "C:\Projects\scripts\", line 25, in <module>
  File "C:\Projects\scripts\", line 86, in doImageTbl
    im =[3].Value))
  File "C:\Python25\lib\site-packages\PIL\", line 1916, in
    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.


More information about the Python-list mailing list