PIL to Qimage conversion: QImage constructor does not free memory
Question: I am developping a Qt application **loading** pictures with **PIL** ,
modifying colors and alpha channels, then **converting them as QImage**.

Here is the problematic piece of code: normal **repeated usage of the
ImageQt** function:

    
    
        # memory is filled  around 7 mB/s
        if **name** == '**main** ':
            while True:
                im = Image.open('einstein.png') #small picture
                imQt = QtGui.QImage(ImageQt.ImageQt(im)) # convert to PySide.QtGui.QImage
                imQt.save('outtest.png')# -> rendered picture is correct
                #del(imQt) and del(im) does not change anything
                time.sleep(0.02)
    

The problem here is the **crazy memory filling** , when the picture is
supposed to be erased by the garbage collector. I checked with gc.collect(),
but it did not change anything.

This example shows what happends with the **imageQt** function, but in fact, I
noticed this is a problem caused by QImage: **if you repeatedly use the QImage
constructor with data, the memory used by python process increases** :

    
    
        im= Image.load('mypic.png').convert('RGBA')
        data = im.toString('raw','RGBA')
        qIm = QtGui.QImage(data,im.size[0],im.size[1],QtGui.QImage.Format_ARGB32)
        qIm.save('myConvertedPic.png')# -> picture is perfect
    

If you put this code in a loop, memory will increase, as 1st example. From
there i am a bit lost because this is a PySide problem...

I tried to use a workaround, but it does not work either:

    
    
        #Workaround, but not working ....  
    
        if **name** == '**main** ':
            while True:
                im = Image.open('einstein.png') #small picture
                imRGBA = im.convert('RGBA') # convert to RGBA
                imRGBA.save('convtest.png') # ->picture is looks perfect
                imBytes = imRGBA.tostring('raw','RGBA')
                #print("size %d %d" % (imRGBA.size[0],imRGBA.size[1]))
                qImage = QtGui.QImage(imRGBA.size[0],imRGBA.size[1],QtGui.QImage.Format_ARGB32) # create new empty picture
                qImage.fill(QtCore.Qt.blue) # fill with blue, otherwise it catches pieces of the picture still in memory
                loaded = qImage.loadFromData(imBytes,'RGBA') # load from raw data
                print("success %d" % loaded)# -> returns 0
                qImage.save('outtest.png')# -> rendered picture is blue
                time.sleep(0.02)
    

`I am really stuck here, if you could help **find a solution with this
workaround** ? Because I'm really stuck here! Also I would like to **discuss
the QImage problem**. Is there any reliable way to free this memory ? Could
the fact I am using python3.2(32bits) be a problem in this case ? Am I the
only one in this case ?

The imports I am using in case of:

    
    
        import time
        import sys
        import PySide
        sys.modules['PyQt4'] = PySide # this little hack allows to solve naming problem when using PIL with Pyside (instead of PyQt4)
        from PIL import Image, ImageQt
        from PySide import QtCore,QtGui
    
    


Answer: After further unsuccessful searching, I noticed, that the PIL function
**image.tostring()** associated with a **QImage constructor** caused this
problem

    
    
        im = Image.open('einstein.png').convert('RGBA')
        data = im.tostring('raw','RGBA') # the tostring() function is out of the loop
        while True:
            imQt = QtGui.QImage(data,im.size[0],im.size[1],QtGui.QImage.Format_ARGB32)
            #imQt.save("testpic.png") #image is valid
            time.sleep(0.01)
            #no memory problem !

I think I am really close to find what is wrong, but I cannot point it out. It
definitely has something to do with the `data` variable being held in memory.

