如何从pyqt5中的字节设置qpixmap

发布于 2025-01-22 06:47:41 字数 513 浏览 3 评论 0原文

如何从图像文件的字节中设置标签的PixMap (“ example.bmp”)。我几乎整天都在搜索和尝试,但是找不到解决方案。

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

或者,也许有一种方法可以将字节的顺序保存到内存中的文件example2.bmp中(buffer或_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)

How to set Pixmap of label from a bytes of an images file ("example.bmp"). 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. 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)?

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜空下最亮的亮点 2025-01-29 06:47:41

最后,我从错误中找到了解决方案。感谢@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)

Finally, i found solution from my mistakes. Thanks to @musicamante for reminding me.

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文