3

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
Alex G.P.
  • 9,609
  • 6
  • 46
  • 81
  • loadFromData should work, it doesn't expect raw pixel data. Are you giving it the right image format? Have you confirmed that the data you're passing in does actually represent a valid image? – Dan Milburn Jul 25 '13 at 10:16
  • @DanMilburn I updated question with source code which does not works for me. No matter what kind of image - jpg or png I trying to load it always fails. – Alex G.P. Jul 25 '13 at 13:17

2 Answers2

1

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!

Alex G.P.
  • 9,609
  • 6
  • 46
  • 81
0

There is QImage::loadFromData for that.

See the docs.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
  • Thank you for reference to C++ Qt, but for Python port Pyside existing only one method `loadFromData` (http://srinikom.github.io/pyside-docs/PySide/QtGui/QImage.html#PySide.QtGui.PySide.QtGui.QImage.loadFromData) which returns `bool`. Anyway, it is not working accordingly to my updated waustion - I've added code sample. – Alex G.P. Jul 25 '13 at 13:19