How to create a wx.Image object from data in memory?

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

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

 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 do is create an image from the 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 ?

Thank you for your help!

+4
source share
2 answers

You can use StringIO to transfer the buffer to 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.

+8
source

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

0
source

All Articles