在PyQt上显示嵌入式图像?

问题描述 投票:1回答:2

假设我的脚本中有图像的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'),如果问不多的话。

注意:我正在使用嵌入式图像,因为我确实希望没有'resources'文件夹。我需要处理的垃圾越少越好。

python image user-interface pyqt
2个回答
2
投票

使用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_()

0
投票

如果我要嵌入.dcm(DICOM)图像该怎么办?

© www.soinside.com 2019 - 2024. All rights reserved.