来自base64的PyQt4 setWindowIcon

时间:2018-09-12 10:52:05

标签: python python-2.7 pyqt pyqt4

由于使用pyinstaller冻结时在PyQt4脚本中包含图像,因此我遇到了很多麻烦,因此我认为base64是一个更好的主意。如何在PyQt4中将其用于setWindowIcon()函数

1 个答案:

答案 0 :(得分:0)

在下一部分中,我将展示如何将base64图像转换为图标:

import sys
from PyQt4 import QtCore, QtGui


def iconFromBase64(base64):
    pixmap = QtGui.QPixmap()
    pixmap.loadFromData(QtCore.QByteArray.fromBase64(base64))
    icon = QtGui.QIcon(pixmap)
    return icon


image_base64 = b"iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEwAACxMBAJqcGAAAArxJREFUWIXtlktsjFEUx3/3mD5U7TRCCEnrOSUSjXZKpzOlNBWxssBCY9MFIZEQLGwkCLESCZaVWIiIVDRB+tTqDBmpMl+FpEiwIQTxaqf3WJhOKp0xD+0sxH/33XvO+f1yv3y5H/zPv5r26nn5CiZZXdyC9vJF841xNaLUqdiFiuQLvFe1DxBzhZwvF/2dL7+P7WmpK8kr+Ji7A3QrVsoQCrE2AvIE4XpEXWfXBR6+TirQUeE+oIajBnITWavaQVW21QQHggCdFUtXqeolRIoT9mC/ouagP+CcSSjQ4XEfAw4lGjI2Fv1mrKkHUNEWwUxNpQ847usNHx4n0Fa5ZLOoXEtxyC8Jy2cBg1CYTh9WN/uCTjOAAFzesmUKKqfSGgKIMD1tOICY0xplC8DMV+FqgQVpD8o8JZ2V7uqYgCr+LMKJMn8TmJ1tAazOjQlgTCTrAmKGYwLG6GC2+QZ9FhNQw81sC4iV2zEB/12nD+jOHt72eIOP+2MC0exTGJp8to1Y69o7+hgT8PWG74vqnkkXELOrJvgoNE4AoDrgnFf0yGSxFT3k63UujF2Lex13eJaeBLN/guknfIHwuIsu4Q9Du8d9zkDjhLCVs/5AeHe8vYQCCtLhcTcZ2P5XcLTJ1+s0GNB4+xJvMWpmyZ3RoNCcOdxefTvHvTMRPMr5c1rqSvIKPuTdQFibFlztraJPZlOp4/zx004qAHBz+fJpOVMjt0WMJzW87SkcKlhfFgp9TVaZ8BWMzYb+/i8mP6ceS1+yWoUHw0MFG1OBQ4onMJquNSuK7PCPLkQWx9u32AEzNOL1h56+S3VmSicwGm9331tVVy3wYjxcn1vNrU0HDmmewGhaK93FxnJHDLMA1PLGlSNVVd2P0r7WMxIAaF29xD3FSieAjKjXe89xMp2VcdorS8vaypetzDp4IvMTCqn5D13V18YAAAAASUVORK5CYII="


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)

    w = QtGui.QWidget()
    icon = iconFromBase64(image_base64)
    w.setWindowIcon(icon)
    w.show()

    sys.exit(app.exec_())

输出:

enter image description here


使用以下脚本,您可以从图像中获取base64:

from PyQt4 import QtCore, QtGui

def Base64ToBytes(filename):
    image = QtGui.QImage(filename)
    ba = QtCore.QByteArray()
    buff = QtCore.QBuffer(ba)
    image.save(buff, "PNG")
    return ba.toBase64().data()

if __name__ == '__main__':
    val = Base64ToBytes("/path/of/image")
    print(val)