0

I want to display a numpy ndarray in wxpython but somehow I can't get it to work...

My image control:

self.imageCtrl = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.BitmapFromImage(img))

My image code:

img = cv2.imread(file, 0)
flag, thresh = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY
# thresh is an ndarray with size [60, 60]
img_wx = wx.ImageFromBuffer(thresh.shape[0], thresh.shape[1], thresh)
self.imageCtrl.SetBitmap(wx.BitmapFromImage(img_wx)

This results in the following error:

Traceback (most recent call last):
File "cropImagesAndLabel.py", line 114, in onStart
self.countUp()
File "cropImagesAndLabel.py", line 134, in countUp
self.crop()
File "cropImagesAndLabel.py", line 104, in crop
img = wx.ImageFromBuffer(self.pattern.shape[0], self.pattern.shape[1], self.pattern)
File "\Canopy\User\lib\site-packages\wx\_core.py", line 3598, in ImageFromBuffer
image = _core_._ImageFromBuffer(width, height, dataBuffer, alphaBuffer)
TypeError: expected a single-segment buffer object

It would be great if anybody could help!

Kind regards, m

1 Answer 1

1

cv2's image is actually a ndarry, you can use Matplotlib window to show the image. You can also put the Matplotlib's window inside a wxPython window, see this as a reference: user_interfaces example code: embedding_in_wx2.py. Note that the link above shows how to display a graph using the plot function. You can use self.axes.imshow(your_image) to show the image. For example, a black image can be constructed by following statement

your_image = np.zeros((512,512,3), 'uint8')

Also, you should note that in the above link, the backend of the matplotlib has set to wxAgg, which means it use wxPython as a drawing backend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.