在PyQt上显示嵌入式图像? [英] Displaying an embedded image on PyQt?

查看:217
本文介绍了在PyQt上显示嵌入式图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Let's say I have the base-64 code of an image in my script like so:

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

如果我可以这样解码它:

If I could decode it like this:

EmbeddedCode.decode('base64')

然后如何在这样的PyQt4 gui中显示它?:

Then how can I display it in a PyQt4 gui like this?:

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

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

Preferably without having to use open('image.jpg','w'), if it's not to much to ask.

注意:我使用的是嵌入式图像,因为我确实希望没有"resources"文件夹.我需要处理的废话越少越好.

Note: I am using embedded images because I'd really rather not have a 'resources' folder. the less crap I have to deal with, the better.

推荐答案

使用QPixmap的 loadFromData 方法:

Use the loadFromData method of QPixmap:

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

这篇关于在PyQt上显示嵌入式图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆