gpt4 book ai didi

python - 在 PyQt 上显示嵌入图像?

转载 作者:行者123 更新时间:2023-12-01 05:11:47 25 4
gpt4 key购买 nike

假设我的脚本中有图像的 base-64 代码,如下所示:

EmbeddedCode = """INSERTCODEHERE.....
.....EXAMPLEEXAMPLE"""

如果我能像这样解码它:

EmbeddedCode.decode('base64')

那么我怎样才能像这样在 PyQt4 gui 中显示它呢?:

pic = QtGui.QLabel(self)
pic.setGeometry(0, 0, 512, 512)
pic.setPixmap(QtGui.QPixmap(IMAGE PATH GOES HERE))

如果要求不高,最好不必使用 open('image.jpg','w')

注意:我使用嵌入图像,因为我真的不想有“资源”文件夹。我需要处理的垃圾越少越好。

最佳答案

使用QPixmap的loadFromData方法:

from PyQt4 import Qt,QtGui,QtCore
import base64

# "b64_data" is a variable containing your base64 encoded jpeg

app = QtGui.QApplication([])
w = QtGui.QWidget()
pic = QtGui.QLabel(w)
pm = QtGui.QPixmap()
pm.loadFromData(base64.b64decode(b64_data))
pic.setPixmap(pm)

w.show()
app.exec_()

关于python - 在 PyQt 上显示嵌入图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24069219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com