Skip to main content
 首页 » 编程设计

python之PySide.QtGui.QImage 到 Base64

2024年11月01日30傻小

很抱歉新手问题,但找不到解决方案。你知道 PySide.QtGui.QImage 转换 base64 图像或将数据转换为字符串吗?

我试过了

image_data = base64.b64encode (imageActual)

但是我得到了这个错误

TypeError: must be string or buffer, not PySide.QtGui.QImage

谢谢

请您参考如下方法:

转换为 base64:

    img = QtGui.QImage('image.png') 
    ba = QtCore.QByteArray() 
    buffer = QtCore.QBuffer(ba) 
    buffer.open(QtCore.QIODevice.WriteOnly) 
    img.save(buffer, 'PNG') 
    base64_data = ba.toBase64().data() 

从 base64 转换:

    ba = QtCore.QByteArray.fromBase64(base64_data) 
    img = QtGui.QImage.fromData(ba, 'PNG')