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

标签 python image user-interface pyqt

假设我的脚本中有图像的 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/

相关文章:

class - 如何检查值是否是 Matlab 中的有效属性?

python - 查询仍在为 django_migrations 运行

python - Pandas 数据帧KeyError : 'the label [2019-01-14] is not in the [index]'

java - 将组件添加到 NetBeans GUI Builder 中的选项板

android - 如何将多张图片分享到 whatsapp 应用程序?

php - 图像不使用路径从数据库获取

html - 用于游戏 UI 的 Angular JS

Python - plotly - 结合气泡和等值线图

python - 第一个未命名参数可选

android - 使用 Picasso 的自定义下载器