Python Decode Base64 qUncompress Image

Hi!

I’m having issues creating a QImage from a base64 string that has been compressed using qCompress. I’ve looked around a lot and can’t seem to find any examples anyone using qUncompress. Python is not my forte in any way, so I apologize if this is a simple question.

Here is my code snippet

def decode_thumbnail(str_image):
    bytearray = QtCore.QByteArray.fromBase64(str_image)
	bytearray = QtCore.qUncompress(bytearray)

	image = QtGui.QImage()
	image.fromData(bytearray, 'PNG')
	return image

If someone could give me some pointers that would be great! Thanks!

Got the solution over on stack overflow.

If someone finds this post, the solution is:

return image.fromData(bytearray, mimeTypme)

fromData does not store the result to image.

That’s because QImage.fromData is a static method.
So you don’t actually have to create an instance of the QImage, instead thing of fromData as an alternative constructor (which it is).

def decode_thumbnail(str_image):
    bytearray = QtCore.QByteArray.fromBase64(str_image)
	bytearray = QtCore.qUncompress(bytearray)
	return QtGui.QImage.fromData(byteArray, 'PNG')
1 Like

Thanks a lot radianceforge. I had been look for a while to do uncompressing of PNGs mostly for reading the Substance File Thumbnails. Tried a lot but never new about qUncompress. Just beautiful.

Thank you.

1 Like