将 blob 图像数据加载到 QPixmap 中
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 QImage.fromData 静态方法从字符串加载图像,然后将其转换为像素图:
You can use the QImage.fromData static method to load an image from a string and then convert it to a pixmap:
Ants Aasma 建议的方法有效,实际上只使用以下代码也可以:
非常感谢所有的帮助和信息。
The approach suggested by Ants Aasma works and actually it is also OK to just use the following code:
Thanks a lot for all the help and information.
经过一个半小时的谷歌搜索来解决类似的问题,我最终使用 QT 在编译的 .exe 中加载 JPEG。 我使用的是 python3.1,因此无法使用前面提到的一些解决方案:
虽然此处发布的解决方案不起作用,但非常相似的解决方案却起作用了:
我只是将
[PythonDir]\Lib\site-packages\PyQt4\plugins\imageformats
复制到我的 exe 文件夹中,并删除了我在该文件夹中创建的qt.conf
文件以下其他解决方案。 这就是全部(我想:p)。之后,无论我使用 QPixmap 的构造函数加载 jpg 还是先加载 QImage ,它都有效。 使用 cxfreeze 编译为 exe 的
setup.py
和cxfreeze.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 :
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 theqt.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 aQImage
first. It also worked with no special option needed for both thesetup.py
and thecxfreeze.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.