Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QImage and scaling problems
Forum Updated to NodeBB v4.3 + New Features

QImage and scaling problems

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 972 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kris Revi
    wrote on 25 Sept 2020, 14:15 last edited by
    #1

    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";
        }
        else
        {
            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
        img2.save(fileName);                      // 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
    Original

    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)

    V 1 Reply Last reply 25 Sept 2020, 14:34
    0
    • K Kris Revi
      25 Sept 2020, 14:15

      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";
          }
          else
          {
              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
          img2.save(fileName);                      // 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
      Original

      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)

      V Offline
      V Offline
      VRonin
      wrote on 25 Sept 2020, 14:34 last edited by
      #2

      @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.

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3

      1/2

      25 Sept 2020, 14:15

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved