How to render grayscale numpy array as PyQt6 QImage?

291 views Asked by At

Given a variable blurmap that is a grayscale 1440x1080 numpy array that when saved with imageio results in this image:

https://i.stack.imgur.com/AxKha.jpg

What is causing PyQt to render it like this?

https://i.stack.imgur.com/8pnlP.png

The relevant code to load the data as a QImage/QPixmap is as follows:

h, w = blurmap.shape
self.blurmap = QtGui.QImage(blurmap, w, h, QtGui.QImage.Format.Format_Grayscale8)
self.image_3.setPixmap(QtGui.QPixmap(self.blurmap))

I've tried various formats and even the above code is already from other questions that have asked similar problems, but I can't seem to get it to render correctly no matter what I do. I am able to load RGB888 files in the same project with slight variation to the code but this greyscale image is not working at all. For example here is the relevant code for loading the RGB888 images which I have also tried to adapt for the grayscale image to no avail:

h, w, c = self.rawInput.shape
bytesPerLine = 3 * w
self.baseImg = QtGui.QImage(self.rawInput.data, w, h, bytesPerLine, QtGui.QImage.Format.Format_RGB888)
self.image_1.setPixmap(QtGui.QPixmap(self.baseImg))

As far as I'm aware the only difference is with the bytesPerLine variable added which I'm to believe is simply taking into account the 3 channels of the RGB image, which this grayscale should not require

0

There are 0 answers