Binary blobs to jpeg

J

jayharvard1

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
 
C

Carsten Haese

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.

Have you tried to open the file in "wb" mode?

HTH,
 
J

jayharvard1

Have you tried to open the file in "wb" mode?

The data is coming from a record set selection from the Access
database. I guess I could write the data to a temp file and open that
file handle with the "wb" mode. But, no, I haven't tried that.

jeh
 
G

Grant Edwards

The data is coming from a record set selection from the Access
database. I guess I could write the data to a temp file and
open that file handle with the "wb" mode. But, no, I haven't
tried that.

I think you missed the point.

When you want to write the binary data to a file, you must open
the destination file using "wb" mode. It's binary data, not
text data, so you have to open the output file in binary mode.
 
J

jayharvard

I think you missed the point.

When you want to write the binary data to a file, you must open
the destination file using "wb" mode. It's binary data, not
text data, so you have to open the output file in binary mode.

I did miss the point. I will give that a shot. Thanks Carsten and Grant
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top