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. Change pixel color of QPixmap
QtWS25 Last Chance

Change pixel color of QPixmap

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 5 Posters 6.9k 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.
  • J Jakob Clausen
    2 May 2022, 09:51

    I have a pixmap, and I would like to change the color of alle pixels with a certain color to another.

    QPixmap IconWidget::changeColor(QPixmap& pixmap, QColor to, QColor from)
    {
        QImage temp(pixmap.toImage());
        for (int y = 0; y < temp.height(); y++)
        {
            for (int x= 0; x < temp.width(); x++)
            {
                to.setAlpha(temp.pixelColor(x, y).alpha());
                from.setAlpha(temp.pixelColor(x, y).alpha());
                if (temp.pixelColor(x, y) == from)
                    temp.setPixelColor(x, y, to);
            }
        }
    
        return QPixmap::fromImage(out);
    }
    

    This works very well.
    But then I found out, that we have to use Qt 5.3. And the functions pixelColor() and setPixelColor() are not introduced before 5.6. So then I thougt to use pixel() and setPixel instead like this:

    QPixmap IconWidget::changeColor(QPixmap& pixmap, QColor to, QColor from)
    {
        QImage temp(pixmap.toImage());
    
        for (int y = 0; y < temp.height(); y++)
        {
            for (int x= 0; x < temp.width(); x++)
            {
                QColor color(temp.pixel(x, y));
    
                to.setAlpha(color.alpha());
                from.setAlpha(color.alpha());
                if (color == from)
                    temp.setPixel(x, y, to.rgb());
            }
        }
    
        return QPixmap::fromImage(temp);
    }
    

    And then the pixmaps look like strange boxes. Some of them has some are white, some are black, som have a stranged distorted content.
    What should I do?
    Is there a better way of doing this?

    K Offline
    K Offline
    KroMignon
    wrote on 2 May 2022, 10:15 last edited by
    #2

    @Jakob-Clausen said in Change pixel color of QPixmap:

    Is there a better way of doing this?

    As written in documentation, you should use QColor::fromRgba() to not loose alpha channel:

    • https://doc.qt.io/qt-5/qcolor.html#QColor-3
    • https://doc.qt.io/qt-5/qcolor.html#fromRgba
    1 Reply Last reply
    3
    • J Offline
      J Offline
      Jakob Clausen
      wrote on 3 May 2022, 07:20 last edited by Jakob Clausen 5 Mar 2022, 07:27
      #3

      Like this?

      QPixmap IconWidget::changeColor(QPixmap& pixmap, QColor to, QColor from)
      {
          QImage temp = pixmap.toImage();
      
          for (int y = 0; y < temp.height(); y++)
          {
              for (int x= 0; x < temp.width(); x++)
              {
                  QColor color = QColor::fromRgba(temp.pixel(x, y));
                  uint8_t alpha = color.alpha();
      
                  to.setAlpha(alpha);
                  from.setAlpha(alpha);
                  uint value = to.rgba();
      
                  if (color == from)
                  {
                      temp.setPixel(x, y, value);
                  }
              }
          }
      
          return QPixmap::fromImage(temp);
      }
      

      It looks better but still strange.

      K 1 Reply Last reply 3 May 2022, 09:30
      0
      • J Jakob Clausen
        3 May 2022, 07:20

        Like this?

        QPixmap IconWidget::changeColor(QPixmap& pixmap, QColor to, QColor from)
        {
            QImage temp = pixmap.toImage();
        
            for (int y = 0; y < temp.height(); y++)
            {
                for (int x= 0; x < temp.width(); x++)
                {
                    QColor color = QColor::fromRgba(temp.pixel(x, y));
                    uint8_t alpha = color.alpha();
        
                    to.setAlpha(alpha);
                    from.setAlpha(alpha);
                    uint value = to.rgba();
        
                    if (color == from)
                    {
                        temp.setPixel(x, y, value);
                    }
                }
            }
        
            return QPixmap::fromImage(temp);
        }
        

        It looks better but still strange.

        K Offline
        K Offline
        KroMignon
        wrote on 3 May 2022, 09:30 last edited by
        #4
        This post is deleted!
        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jakob Clausen
          wrote on 4 May 2022, 10:37 last edited by
          #5

          Anyone? Is there another way of doing this?

          C 1 Reply Last reply 4 May 2022, 10:40
          0
          • J Jakob Clausen
            4 May 2022, 10:37

            Anyone? Is there another way of doing this?

            C Online
            C Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 4 May 2022, 10:40 last edited by
            #6

            @Jakob-Clausen said in Change pixel color of QPixmap:

            Is there another way of doing this?

            Without relying on internals? No. You have to go over all pixels, no matter if you're doing it your way or some smarter by relying on internal stuff.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            J 1 Reply Last reply 4 May 2022, 11:10
            0
            • C Christian Ehrlicher
              4 May 2022, 10:40

              @Jakob-Clausen said in Change pixel color of QPixmap:

              Is there another way of doing this?

              Without relying on internals? No. You have to go over all pixels, no matter if you're doing it your way or some smarter by relying on internal stuff.

              J Offline
              J Offline
              Jakob Clausen
              wrote on 4 May 2022, 11:10 last edited by
              #7

              @Christian-Ehrlicher Can you see what I am doing wrong. Why is it not working?

              C 1 Reply Last reply 4 May 2022, 11:24
              0
              • J Jakob Clausen
                4 May 2022, 11:10

                @Christian-Ehrlicher Can you see what I am doing wrong. Why is it not working?

                C Online
                C Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 4 May 2022, 11:24 last edited by
                #8

                @Jakob-Clausen said in Change pixel color of QPixmap:

                Why is it not working?

                Don't know - add some debug output, use a debugger and step trough to see the values. Do what a software developer does in such a case.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                J 1 Reply Last reply 4 May 2022, 11:37
                0
                • C Christian Ehrlicher
                  4 May 2022, 11:24

                  @Jakob-Clausen said in Change pixel color of QPixmap:

                  Why is it not working?

                  Don't know - add some debug output, use a debugger and step trough to see the values. Do what a software developer does in such a case.

                  J Offline
                  J Offline
                  Jakob Clausen
                  wrote on 4 May 2022, 11:37 last edited by
                  #9

                  @Christian-Ehrlicher I don't understand. How is a debugger going to help me? What do you think I should look for?

                  C 1 Reply Last reply 4 May 2022, 12:00
                  0
                  • J Jakob Clausen
                    4 May 2022, 11:37

                    @Christian-Ehrlicher I don't understand. How is a debugger going to help me? What do you think I should look for?

                    C Online
                    C Online
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 4 May 2022, 12:00 last edited by
                    #10

                    @Jakob-Clausen said in Change pixel color of QPixmap:

                    What do you think I should look for?

                    You should look if e.g. setPixel() is called ...

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    J 1 Reply Last reply 4 May 2022, 12:17
                    0
                    • C Christian Ehrlicher
                      4 May 2022, 12:00

                      @Jakob-Clausen said in Change pixel color of QPixmap:

                      What do you think I should look for?

                      You should look if e.g. setPixel() is called ...

                      J Offline
                      J Offline
                      Jakob Clausen
                      wrote on 4 May 2022, 12:17 last edited by
                      #11

                      @Christian-Ehrlicher It is being called. If I remove the line, then the picture looks like the original. For me setPixel() is a magic function that should replace one pixel color with another. If it does not, then I am lost.

                      J 1 Reply Last reply 4 May 2022, 12:36
                      0
                      • C Online
                        C Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 4 May 2022, 12:19 last edited by
                        #12

                        So do you actually use the pixmap returned by the function?

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        J 1 Reply Last reply 4 May 2022, 13:15
                        1
                        • K Offline
                          K Offline
                          kkoehne
                          Moderators
                          wrote on 4 May 2022, 12:36 last edited by
                          #13

                          Note the caveats mentioned in https://doc.qt.io/qt-5/qimage.html#setPixel :

                          If the image's format is either monochrome or paletted, the given index_or_rgb value must be an index in
                          the image's color table, otherwise the parameter must be a QRgb value.
                          
                          If position is not a valid coordinate pair in the image, or if index_or_rgb >= colorCount() in the case of monochrome and paletted images, the result is undefined.
                          

                          So, is your image maybe using a palette? There's more details here: https://doc.qt.io/qt-5/qimage.html#pixel-manipulation

                          Director R&D, The Qt Company

                          1 Reply Last reply
                          2
                          • J Jakob Clausen
                            4 May 2022, 12:17

                            @Christian-Ehrlicher It is being called. If I remove the line, then the picture looks like the original. For me setPixel() is a magic function that should replace one pixel color with another. If it does not, then I am lost.

                            J Online
                            J Online
                            JonB
                            wrote on 4 May 2022, 12:36 last edited by
                            #14

                            @Jakob-Clausen
                            As @Christian-Ehrlicher has just asked --- if you think your code is changing the passed-in QPixmap& pixmap, it is not!

                            1 Reply Last reply
                            1
                            • C Christian Ehrlicher
                              4 May 2022, 12:19

                              So do you actually use the pixmap returned by the function?

                              J Offline
                              J Offline
                              Jakob Clausen
                              wrote on 4 May 2022, 13:15 last edited by
                              #15

                              @Christian-Ehrlicher yes.

                              1 Reply Last reply
                              0
                              • J Offline
                                J Offline
                                Jakob Clausen
                                wrote on 4 May 2022, 13:31 last edited by
                                #16

                                If I comment out the line setPixel(), then the picture will look like the original:
                                original.png
                                If I do as described, then it looks like this:
                                distorted.png
                                So it is changing the pixelvalues. But it is a little rough on the edges.

                                K 1 Reply Last reply 4 May 2022, 13:58
                                0
                                • C Online
                                  C Online
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on 4 May 2022, 13:39 last edited by
                                  #17

                                  Try to use setPixelColor(int x, int y, const QColor &color).

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  J 1 Reply Last reply 4 May 2022, 13:47
                                  0
                                  • C Christian Ehrlicher
                                    4 May 2022, 13:39

                                    Try to use setPixelColor(int x, int y, const QColor &color).

                                    J Offline
                                    J Offline
                                    Jakob Clausen
                                    wrote on 4 May 2022, 13:47 last edited by
                                    #18

                                    @Christian-Ehrlicher If I do that, then it works perfectly. But setPixelColor() is introduced before 5.6, and I am forced to use 5.3.

                                    C 1 Reply Last reply 4 May 2022, 13:49
                                    0
                                    • J Jakob Clausen
                                      4 May 2022, 13:47

                                      @Christian-Ehrlicher If I do that, then it works perfectly. But setPixelColor() is introduced before 5.6, and I am forced to use 5.3.

                                      C Online
                                      C Online
                                      Christian Ehrlicher
                                      Lifetime Qt Champion
                                      wrote on 4 May 2022, 13:49 last edited by
                                      #19

                                      @Jakob-Clausen Did you read @kkoehne 's comment? What pixel format do you have?

                                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                      Visit the Qt Academy at https://academy.qt.io/catalog

                                      J 1 Reply Last reply 4 May 2022, 13:57
                                      0
                                      • C Christian Ehrlicher
                                        4 May 2022, 13:49

                                        @Jakob-Clausen Did you read @kkoehne 's comment? What pixel format do you have?

                                        J Offline
                                        J Offline
                                        Jakob Clausen
                                        wrote on 4 May 2022, 13:57 last edited by
                                        #20

                                        @Christian-Ehrlicher QImage::Format_ARGB32_Premultiplied

                                        1 Reply Last reply
                                        0
                                        • J Jakob Clausen
                                          4 May 2022, 13:31

                                          If I comment out the line setPixel(), then the picture will look like the original:
                                          original.png
                                          If I do as described, then it looks like this:
                                          distorted.png
                                          So it is changing the pixelvalues. But it is a little rough on the edges.

                                          K Offline
                                          K Offline
                                          KroMignon
                                          wrote on 4 May 2022, 13:58 last edited by KroMignon 5 Apr 2022, 14:00
                                          #21

                                          @Jakob-Clausen Perhaps you should try to force pixel format to what you need:

                                          QPixmap IconWidget::changeColor(QPixmap& pixmap, QColor to, QColor from)
                                          {
                                              QImage temp = pixmap.toImage();
                                              
                                              qDebug() << "Pixel format is:" << temp.pixelFormat();
                                              
                                              if(temp.pixelFormat() != QImage::Format_ARGB32)
                                                  temp.convertTo(QImage::Format_ARGB32);
                                              
                                              for (int y = 0; y < temp.height(); ++y)
                                              {
                                                  QRgb* s = reinterpret_cast<QRgb *>(temp.scanLine(y));
                                          
                                                  for (int x= 0; x < temp.width(); ++x, ++s)
                                                  {
                                                      QColor color = QColor::fromRgba(*s);
                                                      uint8_t alpha = color.alpha();
                                          
                                                      to.setAlpha(alpha);
                                                      from.setAlpha(alpha);
                                          
                                                      if (color == from)
                                                         *s = to.rgba();
                                                  }
                                              }
                                          
                                              return QPixmap::fromImage(temp);
                                          }
                                          
                                          J 1 Reply Last reply 4 May 2022, 14:19
                                          0

                                          11/23

                                          4 May 2022, 12:17

                                          • Login

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