如何在 PyQt5 中从字节设置 QPixmap

How to Set QPixmap from Bytes in PyQt5

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

我想设置标签的像素图以显示来自“Bytes”源的图像,但我不想将图像文件保存在磁盘上。有办法解决这个问题吗?

或者有没有办法将字节序列保存到内存中的文件example2.bmp(缓冲区或_io.BufferedReader)?

这是我的代码

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)