如何在PyQt5中从字节设置QPixmap



如何从图像文件("example.bmp")的一个字节设置标签的像素图。我几乎一整天都在寻找、尝试各种方法,但就是找不到解决办法。

我想设置标签的Pixmap来显示来自">Bytes的图像";源代码,但我不想将映像文件保存在磁盘上。有办法解决这个问题吗?

或者是否有一种方法可以将字节序列保存到内存(缓冲区或_io.BufferedReader)中的文件example2.bmp中?

我的代码

with open("example.bmp", 'rb') as file:
header = file.read(53)
pixeldata = file.read()
images = header+pixel #Bytes sequence of example.bmp file
pixmap = QPixmap.loadFromData(images)
self.label.setPixmap(pixmap)

我终于从错误中找到了解决办法。谢谢@musicamante提醒我。

with open("example.bmp", 'rb') as file:
header = file.read(53)
pixeldata = file.read()
images = header+pixel #Bytes sequence of example.bmp file
# Create QPixmap instance
pixmap = QPixmap()
# Put bytes of example.bmp into it
pixmap.loadFromData(images)
# Apply that to the label
self.label.setPixmap(pixmap)

最新更新