views:

1436

answers:

3

Hi, 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

+2  A: 

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)
Ants Aasma
+1  A: 

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.

Bing Jian
A: 

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.

Lay