pythonpython-3.xpyqtpyqt5qimage

PyQt5 QImage isn't reading the image from 2D grayscale array correctly


My code down gave me odd results, as seen in the attached photo QImage to the left, and ndarray to the right.

How can I solve the problem of transforming 2D grayscale ndarray to QImage correctly please?

qimage = QImage(Image, Image.shape[0],Image.shape[1],QImage.Format_Mono).scaled(308, 384, Qt.KeepAspectRatio, Qt.FastTransformation) 
print(Image.shape)
plt.imshow(Image,cmap=plt.cm.bone)
plt.show()
self.image = QPixmap(qimage)
self.LB_Image_Orig.setPixmap(self.image)
self.LB_Image_Orig.adjustSize()
QApplication.processEvents()

Solution

  • Thanks to eyllanesc for his proposed solution which was to write the image as PNG and read it directly from disk:

    from skimage.io import imsave, imread
    
    imsave('image.png', Image.astype(np.float))
    self.image = QPixmap('image.png')
    self.LB_Image_Orig.setPixmap(self.image)
    self.LB_Image_Orig.adjustSize()
    QApplication.processEvents()
    
    if os.path.exists("image.png"):
     os.remove("image.png")