[英]How to Set QPixmap from Bytes in PyQt5
How to set Pixmap of label from a bytes of an images file ("example.bmp")
.如何从图像文件("example.bmp")
的一个字节设置 label 的像素图。 I have searched and tried every way almost all day, but I can't find a solution.我几乎整天都在搜索和尝试各种方法,但找不到解决方案。
I want to set the Pixmap of a label to display the image from the " Bytes " source, but I don't want to save the image file on disk.我想设置 label 的 Pixmap 以显示来自“ Bytes ”源的图像,但我不想将图像文件保存在磁盘上。 Is there a way to fix this problem?有没有办法解决这个问题?
Or maybe is there a way how to save the sequence of bytes into a file example2.bmp
in memory (buffer or _io.BufferedReader)?或者有没有办法将字节序列保存到 memory(缓冲区或 _io.BufferedReader)中的文件example2.bmp
中?
Here is my code这是我的代码
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)
Finally, i found solution from my mistakes.最后,我从错误中找到了解决方案。 Thanks to @musicamante for reminding me.感谢@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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.