def to_base64(self, w=32, h=32): """Return icon as base64 to make it work with html""" pix = self.pixmap(w, h) data = QByteArray() buff = QBuffer(data) pix.save(buff, "PNG") return data.toBase64().data().decode()
def encode_image(image: QImage) -> str: image_bytes = QByteArray() buffer = QBuffer(image_bytes) buffer.open(QIODevice.WriteOnly) # writes pixmap into bytes in PNG format image.save(buffer, "PNG") # type: ignore encoded_bytes = image_bytes.toBase64() codec = QTextCodec.codecForName(b"UTF-8") encoded_string = codec.toUnicode(encoded_bytes) return encoded_string
def QImageToBase64(image: QImage, format: str = "jpeg", width: int = 1080, with_header: bool = True) -> str: byte_array = QByteArray() buffer = QBuffer(byte_array) image.scaledToWidth(width).save(buffer, format) result = "" if with_header: result += "data:image/%s;base64," % format result += str(byte_array.toBase64())[2:-1] return result
def prepareRequest(self): request = QNetworkRequest() request.setUrl(self.urlServer) request.setRawHeader(QByteArray(b"content-type"), QByteArray(b"application/json")) request.setRawHeader(QByteArray(b"charset"), QByteArray(b"utf-8")) if self.mpid != "open@": tokenStr = self.usuario + ":" + str(int( self.token[0])) + ":" + str( self.token[1]) if self.token != [] else "" tokenByte = QByteArray(tokenStr.encode()) tokenByteComplete = QByteArray(b"Session ") + tokenByte.toBase64() request.setRawHeader(QByteArray(b"authorization"), tokenByteComplete) return request