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 a black pixel into a blue one
Forum Updated to NodeBB v4.3 + New Features

Change a black pixel into a blue one

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 9.1k Views 1 Watching
  • 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.
  • I Offline
    I Offline
    icon444
    wrote on last edited by
    #1

    Hi guys,

    I've wrote this piece of code that should make the black part of an image into a blue part. But the code doesn't change the black into the blue it never comes in the if test I think.

    Could someone help me?

    @ QImage background;
    QImage world(1500, 768, QImage::Format_RGB32);
    QSize sizeImage;
    int height, width;
    background.load("Background.png");
    world.fill(1);

    QPainter painter(&world);
    sizeImage = background.size();
    width = sizeImage.width();
    height = sizeImage.height();
    
    const QRgb black = 0;
    const QRgb blue = 255;
    for(int y = 0; y < height; y++) {
        for(int x = 0; x < width; x++) {
            if (background.pixel(x,y) == black) {
                background.setPixel(x,y,blue);
            }
        }
    }
    
    painter.drawImage(0,0,background);
    
    //adding new image to the graphicsScene
    QGraphicsPixmapItem item( QPixmap::fromImage(background));
    QGraphicsScene* scene = new QGraphicsScene;
    scene->addItem(&item);
    
    QGraphicsView view(scene);
    view.show();@
    

    Kind regards,

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      Qt has "predefined color values":http://developer.qt.nokia.com/doc/qt-4.8/qt.html#GlobalColor-enum
      Those might be easier to use.
      Once again the question: are you sure that your black pixels are absolutely black as you have defined? The values might be only almost black and then your if statement will not work.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • I Offline
        I Offline
        icon444
        wrote on last edited by
        #3

        The are 000000 hex value in illustrator so I think they are black not?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mlong
          wrote on last edited by
          #4

          If they have an alpha channel, then it would be added in (probably as 255 for completely opaque.)

          You may want to iterate your image, and more closely examine the resultant QRgb returned by pixel() to check the r,g,b values rather than assuming a numeric constant.

          Software Engineer
          My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

          1 Reply Last reply
          0
          • I Offline
            I Offline
            icon444
            wrote on last edited by
            #5

            Thanks mlong, that helped the if test works now, but the repainting not, this my code can you please help me?

            @ QImage background;
            QImage world(1500, 768, QImage::Format_RGB32);
            QSize sizeImage;
            int r,g,b,alpha;
            QColor color;
            int height, width;
            background.load("Background.png");
            world.fill(1);

            QPainter painter(&world);
            sizeImage = background.size();
            width = sizeImage.width();
            height = sizeImage.height();
            
            const QRgb black = 0;
            const QRgb blue = 255;
            for(int y = 0; y < height; y++) {
                for(int x = 0; x < width; x++) {
                    color = background.pixel(x,y);
                    color.getRgb(&r,&g,&b,&alpha);
                    if (r == 0 && g == 0 && b == 0) {
                        cout << "Gelukt";
                        background.setPixel(x,y,Qt::blue);
                    }
                }
            }
            
            painter.drawImage(0,0,background);
            
            //adding new image to the graphicsScene
            QGraphicsPixmapItem item( QPixmap::fromImage(world));
            QGraphicsScene* scene = new QGraphicsScene;
            scene->addItem(&item);
            
            QGraphicsView view(scene);
            view.show();@
            
            1 Reply Last reply
            0
            • M Offline
              M Offline
              mlong
              wrote on last edited by
              #6

              You're confusing QColor with QRgb.

              Try something like:
              @
              ...
              QRgb color; // Not QColor
              ...
              color = background.pixel(x,y);
              if (qRed(color) == 0 && qGreen(color) == 0 && qBlue(color) == 0) {
              background.setPixel(x,y,QColor(Qt::blue).rgb()); // Qt::blue is a QColor constant.
              }
              ...
              @

              Brain to terminal. Just an example. YMMV

              Software Engineer
              My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

              1 Reply Last reply
              0
              • I Offline
                I Offline
                icon444
                wrote on last edited by
                #7

                Thank you so much !! :-)

                1 Reply Last reply
                0

                • Login

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