Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QImage from file content

Tags:

python

qt

pyside

I have a table in my DB which contains information about images (like width, height, content-type, file-type and file content). In column file_content stored entire image (not pixel data or something else - entire file readed and stored as binary data). Now I want to create QImage (or QPixmap) from this record in my application on Python+PySide. How can I do it?

I tried loadFromData, but it is expects raw pixel data, not file with header like in my case.

Actually, I have no idea hot to solve it.

UPD: My code sample which does not works:

    with open('Koala.jpg', 'r') as f:
        content = f.read()

    self.image = QtGui.QImage()

    print self.image.loadFromData(content)

Result:

False
Corrupt JPEG data: 1 extraneous bytes before marker 0xd9
JPEG datastream contains no image
like image 520
Alex G.P. Avatar asked Sep 03 '25 09:09

Alex G.P.


1 Answers

Such a silly mistake! Just replaced with open('Koala.jpg', 'r') as f: with with open('Koala.jpg', 'rb') as f: and loadFromData loaded my images.

Never forget to open image files as binary!

like image 159
Alex G.P. Avatar answered Sep 04 '25 22:09

Alex G.P.