Search code examples
pythonnumpypyqtqpixmap

PyQT, error to load data from a numpy array


I am getting an error violation access when I try to set a pixmap from a numpy array.

0xC0000005: Access violation reading location 0x0ca00020

The utilization of a numpy array is a requirement.... Anyway i wouldn't give problems

This is the code, the error is in the setPixmap operation.

    from scipy import misc 
    numpy_image_uint8 = misc.imread('test.jpeg')
    #info_image=images[0]    
    #numpy_image_uint8=info_image.frames[0]
    numpy_image_uint32 = numpy_image_uint8.astype(np.uint32).copy()
    img = (255 << 24 | numpy_image_uint32[:,:,0] << 16 | numpy_image_uint32[:,:,1] << 8 | numpy_image_uint32[:,:,2]).flatten() # pack RGB values 
    imgQ = QImage(img,640,480,QImage.Format_RGB32)
    #imgQ = QImage(QtCore.QString('test.jpeg'))
    self.item.setPixmap(QPixmap.fromImage(imgQ))

Furthermore, two interesting points:

  • If i use a loaded QImage from a file, it works, like this:

    imgQ = QImage(QtCore.QString('test.jpeg'))
    
  • If i save the imgQ variable, the saved image seems correct:

    imgQ = QImage(img,640,480,QImage.Format_RGB32)
    imgQ.save("test_image.bmp")
    

Solution

  • Because imgQ shares memory with img, you need keep img alive.

    try this:

    self.img = img
    imgQ = QImage(self.img,640,480,QImage.Format_RGB32)
    

    Can you give more information that why you do this?