1

I have a QImage and need to convert the bits to grayscale.

Right now I´m just calculating the average of the RGB and writing it into the pixel attributes:

Img = QtGui.QImage(300,600, QtGui.QImage.Format_RGB888)

    for x in range(299):
        for y in range(599):
            gray = self.npPixmap[y][x] * 256
            Img.setPixel(x, y, QtGui.QColor(gray,gray,gray).rgb())

This needs around 1,5s and I would like to half the time.

In this c++ question is a solution with scan each line of the picture and convert each line at once. Unfortunately I was not able to adapt this to python. I could not find a function like

reinterpret_cast<QRgb*>(scan + jj*depth)

Thanks!

Community
  • 1
  • 1
Saorsa
  • 23
  • 6

1 Answers1

0

For my own imaging platform I use ITU-R 601-2 luma transform to convert a color image to a greyscale image. I don't use the average because the human eye is most sensitive for green then red and then blue. If the images is loaded as a numpy array i do the following:

  Images = 0.299*Images[:,:,0]+0.587*Images[:,:,1]+0.114*Images[:,:,2]

So you can use this if you get the array from pyqt and then set the array again.

J. Goedhart
  • 209
  • 1
  • 14