wxpython Image 对象在使用 wx.Image(width, height, data) 构造函数实例化它时期望数据是什么格式

What format does the wxpython Image object expect for data when instantiating it using the wx.Image(width, height, data) constructor

我正在尝试通过指定每个像素来生成图像。为此,我编写了这个小测试来查看它是如何工作的,显然我没有使用正确的数据格式。

import numpy as np
import wx

class Test(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(Test, self).__init__(*args, **kwargs)
        self.initialize()

    def initialize(self):
        self.SetSize(500, 500)
        self.SetTitle("Test")
    
        panel = wx.Panel(self)
    
        width = 500
        height = 500
    
        image_data = np.random.randint(0, 256, size=(width, height, 3))
        print(image)
        image = wx.Image(width = width, height = height, data = image_data)
        bitmap = image.ConvertToBitmap()
    
        wx.StaticBitmap(panel, bitmap = bitmap, size = (500, 500))

def main():
    app = wx.App()
    window = Test(None, style=wx.DEFAULT_FRAME_STYLE ^ wx.RESIZE_BORDER)
    print(type(window))
    window.Show()
    app.MainLoop()

if __name__ == "__main__":
    main()

此代码会打开 window,显示带有黑色、红色、蓝色和绿色像素的条纹彩色图像。相反,我本来希望每个像素都是随机颜色(不仅仅是红色、蓝色和绿色),而且纯黑色的像素要少得多。 wxpython 站点和原始 wxwidgets 站点上的文档只说“数据”应该是“RGB 格式”,我认为我已经提供了我使用的方法。我在这里做错了什么?

编辑: Example output of the code above

正如其中一条评论已经提到的,wxwidgets 的文档在其原始 C 实现中要求使用无符号字符数组。本质上,Image 对象期望其数据采用一种格式,其中每个像素由三个字节给出,每个字节指定 RGB 图像的一个通道的值。

因此,字节对象或数据类型为 ubyte 的 numpy 数组都适用于此。使用 int 将导致 int 被重新解释为单独的字节,这将导致原始 post.

中显示的条纹图像