[wxPython-users] using wxImage in C++ python extension

Robin Dunn robin at alldunn.com
Mon Aug 14 18:56:33 PDT 2006


Christopher Barker wrote:
> Robin Dunn wrote:
>> OTOH, if you don't mind an extra data copy then your extension could 
>> just provide the image data as a string or RGB bytes and then you can 
>> create the wx.Image from Python using that string and the 
>> wx.ImageFromData constructor.
> 
> Why does that have to create a copy? 

Actually, I was thinking of the copy made when making the PyString.  I 
wasn't thinking of making a buffer object, that would definitely be the 
way to go.

> It looks like the C++ 
> wx.ImageSetData() method is expecting a pointer to the data. Couldn't 
> the wxPython version pass the pointer through from the buffer object? Or 
> better yet, from a nd-array (from the numpy array protocol).
> 
> I partly ask this because there is a thread on the matplotlib list about 
> how to make the wx back-end pure Python, rather than having them depend 
> on the right wx-devel stuff being installed, and after building, being 
> as version specific as they are. Being able to pass data into a wxImage 
> (or better yet, a wxBitmap) without copying would be a great help.

wx.Image.SetData makes a copy and gives ownership of the copy to the 
image.  This allows it to work even when the source buffer doesn't live 
as long as the image does.  There is also the wx.Image.SetDataBuffer 
method, which will have the wxImage use the buffer passed in without 
making a copy, but if the buffer doesn't live as long as the image does 
it will likely cause a crash.

-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the wxpython-users mailing list