Important: Please read the Qt Code of Conduct -

QImage and scaling problems

  • This is my code

    void MainWindow::on_BUTTON_SELECTIMAGE_MATRIX_clicked()
        // Open QFileDialog and store the path in fileName
        QString filePath = QFileDialog::getOpenFileName(this, tr("Open Image"), "c\\",
                                                              tr("Image Files (*.png *.jpg *.bmp)"));
        ui->TEXT_IMG_PATH->setText(filePath); // Display the path to the image selected
        imageObject = new QImage();           // Make a new imageObject
        imageObject->load(filePath);          // Load the image from path
        if(!filePath.isEmpty())               // Check to see that a image is selected and stored in fileName
            QFile f(filePath);
            QFileInfo fileInfo(f.fileName());
            QString filename(fileInfo.fileName());
            imageHandler(imageObject, filename);        // Send the Image for processing
    void MainWindow::imageHandler(QImage *img, QString fileName)
        if(img->size() == QSize(32, 32))
            qDebug() << "[IMAGEHANDLER][INFO] 1. Image is 32x32";
            qDebug() << "[IMAGEHANDLER][INFO] 1. Image is NOT 32x32";
            qDebug() << "[IMAGEHANDLER][INFO] 2. Resizing Image to 32x32";
            img->scaled(32, 32, Qt::KeepAspectRatio, Qt::SmoothTransformation); // Scale the image down to 32x32
        QImage img2{32, 32, QImage::Format_RGB16}; // The image is stored using a 16-bit RGB format (5-6-5).
        img2.fill(QColor(Qt::black).rgb());        // Fill the new image with a black background (needs to be filled to get the correct color data)
        QPainter painter(&img2);                   // Prepare new image for painting
        painter.drawImage(0, 0, *img);             // Paint the original image onto the new image;                      // Save that fucker and lets move on
        // Get the color 16bit (5-6-5) color data and put it in a QVector and prepare for sending to ESP32
        QVector<quint16> RGB565;
        RGB565.reserve(img2.width() * img2.height());
        for(int y = 0; y < img2.height(); y++) {
            const quint16 *line = reinterpret_cast<const quint16*>(img2.constScanLine(y));
            for(int x = 0; x < img2.width(); x++)
                RGB565 << *(line++);

    Original Picture

    what i get
    alt text

    so what am i doing wrong? (the size of the image is 32x32 and alpha channel is removed and replaced with a black background! that is correct! however as you can see in the right bottom corner you see alittle red from marios hat so the image did not get scaled down)

  • @Kris-Revi said in QImage and scaling problems:

    img->scaled(32, 32, Qt::KeepAspectRatio, Qt::SmoothTransformation);

    scaled() is const. it doesn't change the image but return a copy of the image scaled.

Log in to reply