0

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")
    

1 Answer 1

2

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?

Sign up to request clarification or add additional context in comments.

4 Comments

I am a newbie with pyqt (and Qt), I am trying to update a GraphicsView in a UI, and this is the source code which i could do work... I didn't know which it is necessary "keep alive" the variables.. Is it a issue related with python and the garbage collector?
Why you load the image into an array, and then convert it to QImage, but don't load the QImage directly from the file? When the garbage collector free the img array, it will release the data area, and QImage will have a wild pointer.
@carlos.baez If you look at the c++ documentation (qt-project.org/doc/qt-4.8/qimage.html) you'll see that the buffer passed to the QImage constructor must remain valid throughout the life of the QImage. Since the QImage exists for as long as the QPixmap does, so must your buffer (img). Note that some Qt classes take ownership of items created (like adding a widget to a layout) so you don't need to hold onto references to prevent Python from garbage collecting them. But in this case Qt does not take ownership of the buffer so you have to keep it around yourself!
@HYRY, This code is a part of an application, i have another module which uses ctypes to load images from a webcam and i use numpy to convert the received data correctly. I agree, the code is confusing because i tried to simulate which i am receiving the image from the camera with numpy instead of loading the image directly...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.