How to display 8-bit grayscale raw image to a QLabel and display it in x11 board?

  • Hi all,

    Currently I'm working on a embedded platform which have Qt 5.2.1 and x11 support. The task involves getting a 300x400 8-bit grayscale single channel raw image from one of the module and display it on a QLabel.

    I read that, there is no Grayscale support available in the Qt5 and hence I have tried with "QImage::Format_Indexed8". I can manage to get the correct data but the colour or look and feel is not at all matching with the actual image content.
    How I can display a 300x400 8-bit grayscale single channel raw image to a QLabel correctly?

    PS: I can share what I have done for this conversion, if anybody interested while giving help.

    Thank you in adavance,
    Ajith P V

  • Moderators

    QImage::Format_Indexed8 requires a corresponding color table. If you haven't set it the result won't be what you expect.
    So you can init the color table like this:

    QImage img(imgData, imgWidth, imgHeight, QImage::Format_Indexed8);
    for( int i = 0; i < 256; ++i )
        img.setColor(i, qRgb(i,i,i));

    Where does your image data come from?

  • @raven-worx Thank you very much for your help. I'm actually using the color table but I think I'm missing something in the conversion. I think it is better to show what I have done for the conversion (please see the below code snippet).

    Step1: Taking the 8-bit single channel grayscale raw image which is stored in the predefined path.

    QFile file8bitImage("<path of the image folder>/8bitImage.raw");
    if (!
    QByteArray array8bitImage = file8bitImage.readAll();
        char* buffer8bitImage =;
        file8bitImage.close(); // close the file after copying the data
    QImage *image = new  QImage(reinterpret_cast<unsigned char*>(buffer8bitImage), 300, 400, QImage::Format_Indexed8);
     QImage resultimage = convertToGray(image); //calling the convert function

    Step 2: Convert logic for display

    QImage MainWindow::convertToGray(QImage *pInputImage)
        if ( pInputImage && !pInputImage->isNull() )
            QImage retImg(pInputImage->width(),pInputImage->height(),QImage::Format_Indexed8);
            QVector<QRgb> table( 256 );
            for( int i = 0; i < 256; ++i )
                table[i] = qRgb(i,i,i);
            for(int i =0; i < pInputImage->width();i++)
                for(int j=0; j < pInputImage->height();j++)
                    QRgb value = pInputImage->pixel(i,j);
            //retImg.invertPixels();  // can used to invert black to white and vice versa
            return retImg;

    Step 3: Displaying the image on QLabel

        int labelWidth = ui->label8bitImage->width();
        int labelHeight = ui->label8bitImage->height();
        QImage scaled_img = resultimage.scaled(labelWidth, labelHeight, Qt::KeepAspectRatio);

    I'm getting the content almost similar to the original but colour and look and feel is entirely different. Please let me know what I'm missing here. If any better conversion is available, I warmly welcome that too.

  • First, look and feel is a very vague thing. You should try to find something concrete, such as that the image is too light, too contrasty or something.

    I think you have not assigned a color table to the input image, which it should need just like the output image. But wouldn't the conversion have been done with that already...

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.