从二进制数据转换为wxbitmap

0 投票
1 回答
1447 浏览
提问于 2025-04-17 09:09

我有一个设备,它把灰度图像存储为一系列8位的无符号整数值。现在我想写一个Python程序,从文件中读取这些图像,并使用wxBitmap来显示它们。我有一段代码可以运行,但感觉效率不高,因为在不同格式之间转换的步骤太多了。

如果有更快的代码建议,我会非常感激。

我现在的代码是:

imagearray=numpy.fromfile(file=self.f, dtype=numpy.uint8, count=npixels).reshape(Height, Width)[::-1]
pilimage = Image.fromarray(imagearray)
rgb= pilimage.convert('RGB')
rgbdata = rgb.tostring()
WxBitmap = wx.EmptyBitmap(Width,Height)            
WxBitmap.CopyFromBuffer(rgbdata)
output=WxBitmap

1 个回答

1

你可以直接从一个numpy数组获取一个wxBitmap。
这是来自wxPyWiki的一个例子:

import wx, numpy

def GetBitmap( self, width=32, height=32, colour = (0,0,0) ):
        array = numpy.zeros( (height, width, 3),'uint8')
        array[:,:,] = colour
        image = wx.EmptyImage(width,height)
        image.SetData( array.tostring())
        wxBitmap = image.ConvertToBitmap()       # OR:  wx.BitmapFromImage(image)
        return wxBitmap

撰写回答