python 将 blob 图像数据加载到 QPixmap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1300908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:52:59  来源:igfitidea点击:

Load blob image data into QPixmap

pythonimagepyqtblobqpixmap

提问by Bing Jian

I am writing a program using PyQt4 for front-end GUI and this program accesses a back-end database (which can be either MySQL or SQLite). I need to store some image data in the database and below is the Python code I use to import image files (in JPEG format) to a blob data field in the database:

我正在使用 PyQt4 为前端 GUI 编写程序,该程序访问后端数据库(可以是 MySQL 或 SQLite)。我需要在数据库中存储一些图像数据,下面是我用来将图像文件(JPEG 格式)导入到数据库中的 blob 数据字段的 Python 代码:

def dump_image(imgfile):
    i = open(imgfile, 'rb')
    i.seek(0)
    w = i.read()
    i.close()
    return cPickle.dumps(w,1)

blob = dump_image(imgfile)
hex_str = blob.encode('hex') 
# x"%s"%hex_str will be the string inserted into the SQL command

This part works fine. My question is about how to create a QPixmap object from the image data stored in the database in PyQt4. My current approach involves the following steps:

这部分工作正常。我的问题是关于如何从存储在 PyQt4 数据库中的图像数据创建 QPixmap 对象。我目前的方法包括以下步骤:

(1) Hex str in database -- cPickle&StringIO --> PIL Image Object

(1) 数据库中的Hex str -- cPickle&StringIO --> PIL Image Object

def load_image(s):
    o = cPickle.loads(s)
    c = StringIO.StringIO()
    c.write(o)
    c.seek(0)
    im = Image.open(c)
    return im

(2) PIL Image Object -->Temporary image file

(2) PIL Image Object -->临时图片文件

(3) Temporary image file --> QPixmap

(3) 临时图片文件 --> QPixmap

This approach also works fine. But it would be better if I don't have to write/read temporary image files which may slow down the program response to user interactions. I guess I could use QPixmap::loadFromData()to directly load from the blob data stored in the database and hope someone here could show me an example on how to use this function.

这种方法也很好用。但是如果我不必写入/读取可能会减慢程序对用户交互的响应的临时图像文件会更好。我想我可以使用QPixmap::loadFromData()直接从存储在数据库中的 blob 数据加载,并希望这里有人可以向我展示如何使用此函数的示例。

TIA,

TIA,

Bing

必应

回答by Ants Aasma

You can use the QImage.fromData static method to load an image from a string and then convert it to a pixmap:

您可以使用 QImage.fromData 静态方法从字符串加载图像,然后将其转换为像素图:

 image_data = get_image_data_from_blob()
 qimg = QtGui.QImage.fromData(image_data)
 pixmap = QtGui.QPixmap.fromImage(qimg)

回答by Bing Jian

The approach suggested by Ants Aasma works and actually it is also OK to just use the following code:

Ants Aasma 建议的方法有效,实际上只使用以下代码也可以:

image_data = cPickle.loads(str(s)) # s is fetched from DB 
qp = QPixmap() 
qp.loadFromData(image_data) 

Thanks a lot for all the help and information.

非常感谢所有的帮助和信息。

回答by Lay

After a good hour and a half of googleing to solve a similar problem, I ended up loading JPEGs in a compiled .exe with QT. I am using python3.1, and therefore could not use some of the previously mentioned solutions :

经过一个半小时的谷歌搜索来解决类似的问题,我最终使用 QT 在编译的 .exe 中加载了 JPEG。我使用的是 python3.1,因此无法使用前面提到的一些解决方案:

  • tips working for py2exe (because I am using cxfreeze instead of py2exe, as py2exe works only for python2),
  • tips that require PIL (also only for python2, afaik).
  • 适用于 py2exe 的提示(因为我使用的是 cxfreeze 而不是 py2exe,因为 py2exe 仅适用于 python2),
  • 需要 PIL 的提示(也仅适用于 python2,afaik)。

While the solutions posted here didn't work, something very similar did : I simply copied the [PythonDir]\Lib\site-packages\PyQt4\plugins\imageformatsto my exe's folder and removed the qt.conffile that I created in that folder following other solutions. That's all (I think :p).

虽然这里发布的解决方案不起作用,但有一些非常相似的事情:我只是将其复制[PythonDir]\Lib\site-packages\PyQt4\plugins\imageformats到我的 exe 文件夹中,并qt.conf按照其他解决方案删除了我在该文件夹中创建的文件。这就是全部(我认为:p)。

After that, it worked whether I loaded the jpg using QPixmap's constructor or loading a QImagefirst. It also worked with no special option needed for both the setup.pyand the cxfreeze.batmethods of compiling to exe using cxfreeze.

之后,无论我是使用QPixmap的构造函数加载 jpg还是QImage先加载 jpg 都有效。对于使用 cxfreeze 编译为 exe的setup.pycxfreeze.bat方法,它也不需要特殊选项。

(this solution was posted by jbz on http://www.thetoryparty.com/wp/2009/08/27/pyqt-and-py2app-seriously-i-dont-know-what-to-do-with-you-when-youre-like-this/)

(此解决方案由 jbz 发布在http://www.thetoryparty.com/wp/2009/08/27/pyqt-and-py2app-seriously-i-dont-know-what-to-do-with-you-当你喜欢这个/)

This question is a bit old, but as the problem seems to be still there, I hope this answer will help python3.1 users out there.

这个问题有点老了,但由于问题似乎仍然存在,我希望这个答案能帮助那里的 python3.1 用户。