WX Bitmap from buffer, confusing invalid buffer size error

49 views Asked by At

I process images as numpy ndarrays, then convert them to WX Bitmaps for display.

If I don't use ndarray.astype first, wx.Bitmap.frombuffer() gives me Invalid data buffer size, and I'm failing to see why.

wx.Bitmap.FromBuffer(image.shape[1], image.shape[0], image.astype(np.uint8))

works

wx.Bitmap.FromBuffer(image.shape[1], image.shape[0], image)

doesn't.

What's confusing is that the image is already an ndarray, of the same shape, in uint8.

npimage_to_wxbitmap - image:
  shape:  (544, 706, 3)
  dtype: uint8
  total bytes: 1152192
npimage_to_wxbitmap - image after astype:
  shape:  (544, 706, 3)
  dtype: uint8
  total bytes: 1152192

npimage_to_wxbitmap - image object type before: <class 'numpy.ndarray'>
npimage_to_wxbitmap - image object type after: <class 'numpy.ndarray'>

I understand that .astype() makes a copy, so maybe that's relevant, but my image is already a deep copy of the source.

It comes from a child process via shared memory. A thread in the parent process spins to pick it up, takes a deep copy, combines it with some metadata as a dict, and puts this in a deque , ready for the GUI thread to pick it up and draw it.

I want to get rid of the .astype for speed. I believe this is 5ms slower than it should be because of the (possibly) unnecessary copying.

Hopefully there's something simple I've misunderstood!

0

There are 0 answers