Search code examples
pythonimageimage-processingwxpythonbuffer

How to instantiate a stream from a buffer in wxPython Phoenix?


Some context:

  • wxPython version 3.0.3.dev1836+f764b32 gtk2 (phoenix)
  • Python 2.7.3
  • Ubuntu 12.04

I am trying to create an image from a stream.
The future image is currently loaded from memory into a buffer.

I have already tried things like:

image_stream = wx.MemoryOutputStream()  # error: no attribute 'MemoryOutputStream'
image_stream.Write( image_buffer, buffer_size )
image = wx.Image( stream )
bitmap = wx.Bitmap( image )

Basically, wx.InputStream, wx.OutputStream and all of their ancestors are abstract-classes, I cannot instantiate them.

As you can see here and there, the documentation for their sub-classes seems to be missing.

It also follows from the error written in the above code that those classes are not implemented yet.

Is there a way to instantiate such stream in wxPython Phoenix? Or a workaround?

By the way, as I will write and then read from the stream, should it be an input stream, an output stream or one and then the other?


Solution

  • wxImage objects can be created directly from any object that supports the Python buffer interface, (bytearrays memoryviews, numpy arrays, etc.) See this section in the Migration Guide and also the __init__ and Create method overloads in the API documentation which have a parameter named data.

    In addition, any Python "file-like object" can be automatically converted to the wx stream classes, so the Image constructor and Create method overloads which accept a stream parameters can use any compatible object (an open file, a StringIO, etc.) automatically, so you shouldn't have any need to make a wx.InputStream or wx.OutputStream.