views:

732

answers:

3

I'm writing a GUI application in Python using wxPython and I want to display an image in a static control (wx.StaticBitmap).

I can use wx.ImageFromStream to load an image from a file, and this works OK:

static_bitmap = wx.StaticBitmap(parent, wx.ID_ANY)
f = open("test.jpg", "rb")
image = wx.ImageFromStream(f)
bitmap = wx.BitmapFromImage(image)
static_bitmap.SetBitmap(bitmap)

But, what I really want to be able to do is create the image from data in memory. So, if I write

f = open("test.jpg", "rb")
data = f.read()

how can I create a wx.Image object from data?

Thanks for your help!

A: 

Since in Python you use Duck Typing you can write your own stream class and hand an instance of that class to ImageFromStream. I think you only need to implement the read method and make it return your data.

PEZ
+5  A: 

You should be able to use StringIO to wrap the buffer in a memory file object.

...
import StringIO

buf = open("test.jpg", "rb").read()
# buf = get_image_data()
sbuf = StringIO.StringIO(buf)

image = wx.ImageFromStream(sbuf)
...

buf can be replaced with any data string.

codelogic
Perfect. Thank you!
ChrisN
A: 

how to do this in c++? create a wx.Image object from in-memory data?

yang