By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,311 Members | 1,197 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,311 IT Pros & Developers. It's quick & easy.

Binary blobs to jpeg

P: n/a
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

Jul 16 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Mon, 2007-07-16 at 18:57 +0000, ja*********@gmail.com wrote:
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,

--
Carsten Haese
http://informixdb.sourceforge.net
Jul 16 '07 #2

P: n/a
Have you tried to open the file in "wb" mode?
>
HTH,

--
Carsten Haesehttp://informixdb.sourceforge.net
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

Jul 16 '07 #3

P: n/a
On 2007-07-16, ja*********@gmail.com <ja*********@gmail.comwrote:
>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.
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.

--
Grant Edwards grante Yow! Is my fallout shelter
at termite proof?
visi.com
Jul 16 '07 #4

P: n/a
On Jul 16, 3:23 pm, Grant Edwards <gra...@visi.comwrote:
On 2007-07-16, jayharva...@gmail.com <jayharva...@gmail.comwrote:
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.

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.

--
Grant Edwards grante Yow! Is my fallout shelter
at termite proof?
visi.com
I did miss the point. I will give that a shot. Thanks Carsten and Grant

Jul 16 '07 #5

P: n/a
On Jul 16, 7:54 pm, jayharv...@gmail.com wrote:
On Jul 16, 3:23 pm, Grant Edwards <gra...@visi.comwrote:


On 2007-07-16, jayharva...@gmail.com <jayharva...@gmail.comwrote:
>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.
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.
--
Grant Edwards grante Yow! Is my fallout shelter
at termite proof?
visi.com

I did miss the point. I will give that a shot. Thanks Carsten and Grant- Hide quoted text -

- Show quoted text -
This worked fantastically. Thanks!

Jul 17 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.