来自文件内容的 QImage [英] QImage from file content
问题描述
我的数据库中有一个表格,其中包含有关图像的信息(如宽度、高度、内容类型、文件类型和文件内容).在 file_content
列中存储了整个图像(不是像素数据或其他东西 - 整个文件被读取并存储为二进制数据).现在我想在 Python+PySide 上的应用程序中从此记录创建 QImage(或 QPixmap).我该怎么做?
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?
我尝试了 loadFromData,但它需要原始像素数据,而不是像我这样的带有标题的文件.
I tried loadFromData, but it is expects raw pixel data, not file with header like in my case.
其实我也不知道热解.
UPD:我的代码示例不起作用:
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)
结果:
False
Corrupt JPEG data: 1 extraneous bytes before marker 0xd9
JPEG datastream contains no image
推荐答案
这种愚蠢的错误!只需将 with open('Koala.jpg', 'r') as f:
替换为 with open('Koala.jpg', 'rb') as f:
和loadFromData
加载了我的图片.
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!
这篇关于来自文件内容的 QImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!