将 blob 图像数据加载到 QPixmap 中

发布于 2024-08-01 21:13:35 字数 1054 浏览 4 评论 0原文

我正在使用 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

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

(1)数据库中的十六进制str -- cPickle&StringIO --> PIL 图像对象

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 图像对象 --> 临时图像文件

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

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

蒂亚,

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:

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:

(1) Hex str in database -- 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

(3) Temporary image file --> 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.

TIA,

Bing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小矜持 2024-08-08 21:13:35

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

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

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

 image_data = get_image_data_from_blob()
 qimg = QtGui.QImage.fromData(image_data)
 pixmap = QtGui.QPixmap.fromImage(qimg)
留蓝 2024-08-08 21:13:35

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

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

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

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

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.

猫弦 2024-08-08 21:13:35

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

  • 适用于 py2exe 的提示(因为我使用 cxfreeze 而不是 py2exe,因为 py2exe 仅适用于 python2)、
  • 需要 PIL 的提示(也仅适用于 python2) , AFAIK)。

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

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

(此解决方案由 jbz 发布于 http://www.thetoryparty.com/wp/2009/08/27/pyqt-and-py2app-seriously-i-dont -know-what-to-do-with-you-when-youre-like-this/

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

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 :

  • 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).

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

After that, it worked whether I loaded the jpg using QPixmap's constructor or loading a QImage first. It also worked with no special option needed for both the setup.py and the cxfreeze.bat methods of compiling to exe using cxfreeze.

(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/)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文