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

Question:

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

    • Edit: The Original Image is here:
  • Original
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()
Asked By: Bilal

||

Answers:

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") 
Answered By: Bilal

use Grayscale8 format:

qimage = QImage(Image, Image.shape[0],Image.shape[1],QImage.Format_Grayscale8).scaled(308, 384, Qt.KeepAspectRatio, Qt.FastTransformation)

Answered By: Assaf-ge