Python buffer to Bitmap

Hello,

I'm saving image files (jpg, gif, png, etc) to a PostgreSQL database 'bytea' fields. The images are saved in native format reading the file and saving it as is.

When i read the database, PostgreSQL return the bytea field as: <type 'buffer'>.

I try to create a wx.Bitmap whith the buffer:

     bitmap = wx.BitmapFromBits(buf, width, height, depth)

but raises an exception:

     File "C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\_gdi.py", line 844, in BitmapFromBits
         val = _gdi_.new_BitmapFromBits(*args, **kwargs)
     TypeError: String required for bits data

At this moment, i avoid the problem saving the buffer to a temp file and creating the bitmap from the file:

     open(tmpfile, "wb").write(buffer)
     wx.Bitmap(tmpfile)
     os.remove(tmpfile)

But I would like to create the bitmap directly from the buffer without using a temp file.

Is there any way to do it?

Thanks,

···

--
*****************************************
Oswaldo Hernández
oswaldo (@) soft-com (.) es
*****************************************
PD:
Antes de imprimir este mensaje, asegúrese de que es necesario.
El medio ambiente está en nuestra mano.

Hi,

Hello,

I'm saving image files (jpg, gif, png, etc) to a PostgreSQL database 'bytea' fields. The images are
saved in native format reading the file and saving it as is.

When i read the database, PostgreSQL return the bytea field as: <type 'buffer'>.

I try to create a wx.Bitmap whith the buffer:

 bitmap = wx\.BitmapFromBits\(buf, width, height, depth\)

but raises an exception:

 File &quot;C:\\Python25\\Lib\\site\-packages\\wx\-2\.8\-msw\-unicode\\wx\\\_gdi\.py&quot;, line 844, in BitmapFromBits
     val = \_gdi\_\.new\_BitmapFromBits\(\*args, \*\*kwargs\)
 TypeError: String required for bits data

At this moment, i avoid the problem saving the buffer to a temp file and creating the bitmap from
the file:

 open\(tmpfile, &quot;wb&quot;\)\.write\(buffer\)
 wx\.Bitmap\(tmpfile\)
 os\.remove\(tmpfile\)

But I would like to create the bitmap directly from the buffer without using a temp file.

Is there any way to do it?

Thanks,
--

I think you'll want to use the stream methods mentioned in this
thread:

So in your case, something like this:

stream = cStringIO.StringIO(buf)
bmp = wx.BitmapFromImage( wx.ImageFromStream( stream ))

Hope that helps.

- Mike

···

On Aug 25, 3:56 am, Oswaldo Hernández <lis...@soft-com.es> wrote:

Mike Driscoll escribió:
...

I think you'll want to use the stream methods mentioned in this
thread:

php - Multi-Page Search Navigation | DaniWeb

So in your case, something like this:

stream = cStringIO.StringIO(buf)
bmp = wx.BitmapFromImage( wx.ImageFromStream( stream ))

Works :slight_smile:

Thanks Mike

···

--
*****************************************
Oswaldo Hernández
oswaldo (@) soft-com (.) es
*****************************************
PD:
Antes de imprimir este mensaje, asegúrese de que es necesario.
El medio ambiente está en nuestra mano.