【问题标题】:Display numpy ndarray in wxpython在 wxpython 中显示 numpy ndarray
【发布时间】:2014-07-31 01:03:38
【问题描述】:

我想在 wxpython 中显示一个 numpy ndarray 但不知何故我无法让它工作......

我的图片控件:

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

我的图片代码:

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)

这会导致以下错误:

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

如果有人能帮忙就太好了!

亲切的问候, 米

【问题讨论】:

    标签: python numpy wxpython


    【解决方案1】:

    cv2 的图像其实是一个ndarry,你可以使用Matplotlib 窗口来显示图像。您也可以将 Matplotlib 的窗口放在 wxPython 窗口中,作为参考:user_interfaces example code: embedding_in_wx2.py。请注意,上面的链接显示了如何使用 plot 函数显示图表。您可以使用self.axes.imshow(your_image) 显示图像。例如,可以通过以下语句构造黑色图像

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

    另外,您应该注意,在上面的链接中,matplotlib 的后端设置为wxAgg,这意味着它使用 wxPython 作为绘图后端。

    【讨论】:

      猜你喜欢
      • 2018-10-10
      • 2021-03-24
      • 2019-06-27
      • 2011-05-02
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      相关资源
      最近更新 更多