QImage from file content

我怕爱的太早我们不能终老 提交于 2019-12-24 00:04:59

问题


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

回答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!




回答2:


There is QImage::loadFromData for that.

See the docs.



来源:https://stackoverflow.com/questions/17851524/qimage-from-file-content

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!