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对象。
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,
蒂雅,
Bing
必应
3 个解决方案
#1
9
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)
#2
3
The approach suggested by Ants Aasma works and actually it is also OK to just use the following code:
ant 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.
非常感谢所有的帮助和信息。
#3
0
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 :
经过一个半小时的google解决类似的问题后,我在一个编译的.exe with QT中加载了jpeg,我使用的是python3.1,因此不能使用之前提到的一些解决方案:
- tips working for py2exe (because I am using cxfreeze instead of py2exe, as py2exe works only for python2),
- 为py2exe工作的提示(因为我使用的是cxfreeze而不是py2exe,因为py2exe只适用于python2),
- tips that require PIL (also only for python2, afaik).
- 需要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\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).
虽然这里发布的解决方案没有效果,但有一些非常相似的做法:我只是简单地将[PythonDir]\Lib\site-package \PyQt4\plugins\imageformat复制到我的exe文件夹中,并删除了我在该文件夹中创建的qt.conf文件。这就是全部(我想: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.
在此之后,它工作了我是否使用QPixmap的构造函数加载jpg或者先加载一个QImage。它也不需要设置的特殊选项。py和cxfreeze。使用cxfreeze编译exe的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- py2app- seriousi -do- do- do- do- you- like- you-when- yourelike -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.
这个问题有点老,但问题似乎还在,我希望这个答案能帮助python3.1的用户。
#1
9
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)
#2
3
The approach suggested by Ants Aasma works and actually it is also OK to just use the following code:
ant 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.
非常感谢所有的帮助和信息。
#3
0
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 :
经过一个半小时的google解决类似的问题后,我在一个编译的.exe with QT中加载了jpeg,我使用的是python3.1,因此不能使用之前提到的一些解决方案:
- tips working for py2exe (because I am using cxfreeze instead of py2exe, as py2exe works only for python2),
- 为py2exe工作的提示(因为我使用的是cxfreeze而不是py2exe,因为py2exe只适用于python2),
- tips that require PIL (also only for python2, afaik).
- 需要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\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).
虽然这里发布的解决方案没有效果,但有一些非常相似的做法:我只是简单地将[PythonDir]\Lib\site-package \PyQt4\plugins\imageformat复制到我的exe文件夹中,并删除了我在该文件夹中创建的qt.conf文件。这就是全部(我想: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.
在此之后,它工作了我是否使用QPixmap的构造函数加载jpg或者先加载一个QImage。它也不需要设置的特殊选项。py和cxfreeze。使用cxfreeze编译exe的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- py2app- seriousi -do- do- do- do- you- like- you-when- yourelike -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.
这个问题有点老,但问题似乎还在,我希望这个答案能帮助python3.1的用户。