Results 1 to 3 of 3

Thread: Change a black pixel to a blue pixel in an image

  1. #1
    Join Date
    Feb 2012
    Posts
    1
    Platforms
    Windows

    Question Change a black pixel to a blue pixel in an image

    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?

    Qt Code:
    1. QImage background;
    2. QImage world(1500, 768, QImage::Format_RGB32);
    3. QSize sizeImage;
    4. int height, width;
    5. background.load("Background.png");
    6. world.fill(1);
    7.  
    8. QPainter painter(&world);
    9. sizeImage = background.size();
    10. width = sizeImage.width();
    11. height = sizeImage.height();
    12.  
    13. const QRgb black = 0;
    14. const QRgb blue = 255;
    15. for(int y = 0; y < height; y++) {
    16. for(int x = 0; x < width; x++) {
    17. if (background.pixel(x,y) == black) {
    18. background.setPixel(x,y,blue);
    19. }
    20. }
    21. }
    22.  
    23. painter.drawImage(0,0,background);
    24.  
    25. //adding new image to the graphicsScene
    26. QGraphicsPixmapItem item( QPixmap::fromImage(background));
    27. scene->addItem(&item);
    28.  
    29. QGraphicsView view(scene);
    30. view.show();
    To copy to clipboard, switch view to plain text mode 

    Kind regards,

  2. #2
    Join Date
    Jan 2012
    Location
    Argentina
    Posts
    167
    Thanks
    33
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Change a black pixel to a blue pixel in an image

    have you define black and blue correctly? seems to be the problem if you are saying that it never enters the IF....try using Qt::black

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Change a black pixel to a blue pixel in an image

    While the use of integers to initialise a QRgb looks odd it is correct language-wise. QRgb is a typedef equivalent to unsigned int.

    It is not working as expected because setting the value this way has set the alpha channel to 0: the colour exists but is 100% transparent and will not match an opaque black/blue pixel. Try setting the colours using the qRgb() macro or, as KillGabio suggests, using symbolic names.
    Last edited by ChrisW67; 17th February 2012 at 06:08.

Similar Threads

  1. Replies: 3
    Last Post: 25th September 2011, 12:04
  2. Replies: 2
    Last Post: 10th June 2011, 15:16
  3. Select pixel from image
    By sergio87 in forum Qt Programming
    Replies: 4
    Last Post: 2nd May 2011, 12:48
  4. QPixmap pixel by pixel ?
    By tommy in forum Qt Programming
    Replies: 19
    Last Post: 3rd December 2007, 23:52
  5. how change the QListBox item position by pixel
    By roy_skyx in forum Qt Programming
    Replies: 2
    Last Post: 20th January 2006, 02:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.