Results 1 to 10 of 10

Thread: Darken QPixmap

  1. #1
    Join Date
    Sep 2019
    Posts
    16
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Darken QPixmap

    Hello !

    I want to know is it possible to darken my images inside the Qt ( using a method ) or do i have to change the whole image with the darken one i made in Paint program?
    I use :

    Qt Code:
    1. Player::Player(QGraphicsPixmapItem *parent)
    2. {
    3. setPixmap(QPixmap(":/Images/PlayerMoveDownStill.png"));
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    I want something like this for example:
    darken.jpg
    Last edited by FlyDoodle; 18th September 2019 at 15:53.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,142
    Thanks
    235
    Thanked 661 Times in 651 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Darken QPixmap

    You could read every pixel from the pixmap (easier to do if you convert the QPixmap into a QImage first), multiply each R, G, and B value by some fraction < 1, then replace it in the image.

    For example, if you used a 90% factor (to get an image 10% darker), a white pixel with R = 255, G = 255, and B = 255 would become a slightly grey pixel with 255 -> 255 * 0.9 = 230. This might not give you the best effect, but it could be done at run time and you would not have to draw an entirely new set of images.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    FlyDoodle (20th September 2019)

  4. #3
    Join Date
    Sep 2019
    Posts
    16
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Darken QPixmap

    Yes something like that is what i was looking for, tho i have one more question, could you please tell me how do i convert QPixmap into QImage ? :3 I'm still learning about Qt and reading Documentation and understanding it is quite troublesome for me.

  5. #4
    Join Date
    Sep 2019
    Posts
    16
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Darken QPixmap

    Actually i think i've found how to convert it to QImage, thank you

  6. #5
    Join Date
    Sep 2019
    Posts
    16
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Darken QPixmap

    I thought i knew how to do it when i was traveling with a train but now that i tried doing it it doesn't work... Can someone please tell me how to do it properly, or what am i missing here... I tried doing something and the picture stay the same :

    Qt Code:
    1. Player::Player(QGraphicsPixmapItem *parent)
    2. {
    3. QPixmap *t = new QPixmap(":/Images/PlayerMoveDownStill.png");
    4. setPixmap(*t);
    5. //setOpacity(0.5);
    6. setPos(game->width()/2-this->boundingRect().width()/2,game->height()/2-this->boundingRect().height()/2);
    7.  
    8. QImage tmp = t->toImage();
    9. QColor color;
    10.  
    11. for(int i=0;i<tmp.height();i++){
    12. for(int k=0;k<tmp.width();k++){
    13. //color.setRgb(255*0.5,255*0.5,255*0.5);
    14. color.setRgb(tmp.pixel(k,i));
    15. //color.setAlpha(tmp.pixelColor(k,i).alpha());
    16. color.setRgb(color.red()*0.5,color.blue()*0.5,color.green()*0.5);
    17. tmp.setPixelColor(k,i,color);
    18. }
    19. }
    20.  
    21. t->fromImage(tmp);
    22. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,142
    Thanks
    235
    Thanked 661 Times in 651 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Darken QPixmap

    First, you do not need to create the QPixmap using "new". The call to setPixmap() will make a copy, so you can simply create the pixmap on the stack. As your code is written now, the QPixmap you create is a memory leak.

    Second, you should call setPixmap() after you modify the image. Because setPixmap() is making a copy, your code sets it with the original, unmodified pixmap.

    Fix your code like this:

    Qt Code:
    1. Player::Player(QGraphicsPixmapItem *parent)
    2. {
    3. // Load the QImage directly from the resource. No need to create a QPixmap first
    4. QImage tmp( ":/Images/PlayerMoveDownStill.png", "PNG" );
    5.  
    6. for(int i=0;i<tmp.height();i++)
    7. {
    8. for(int k=0;k<tmp.width();k++)
    9. {
    10. QColor color( tmp.pixelColor( k, i ) );
    11. color.setRgb(color.red()*0.5,color.blue()*0.5,color.green()*0.5);
    12. tmp.setPixelColor(k,i,color);
    13. }
    14. }
    15.  
    16. // Now, convert the image to a pixmap and set it on the graphics object
    17. QPixmap t = QPixmap::fromImage( tmp );
    18. setPixmap( t );
    19. setPos(game->width()/2-this->boundingRect().width()/2,game->height()/2-this->boundingRect().height()/2);
    20. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  8. The following user says thank you to d_stranz for this useful post:

    FlyDoodle (20th September 2019)

  9. #7
    Join Date
    Sep 2019
    Posts
    16
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Darken QPixmap

    Normally i just do this when using QPixmap :
    Qt Code:
    1. setPixmap(QPixmap("filename"));
    To copy to clipboard, switch view to plain text mode 

    tho i didn't know setPixmap makes a copy of QPixmap, thank you for that.
    And i tried your code and it worked well, thank you but i have one more problem. First picture was transparent but this one after converting it back to Pixmap seems to not be ( background is black ) and i looked for a solution on web and i found out that this line of code is supposed to make it transparent :
    Qt Code:
    1. tmp.fill(qRgba(0,0,0,0));
    To copy to clipboard, switch view to plain text mode 

    But Format_RGB32 doesn't support transparency and i don't know how to set the image to a different format ( Format_ARGB32 ), tried putting it into constructor but none of the constructors have filename and format type as paramaters.
    This is how the darken image looks :
    darkenPlayer.jpg

  10. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,142
    Thanks
    235
    Thanked 661 Times in 651 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Darken QPixmap

    In the code that modifies the QImage pixel, check first to see if the alpha channel is zero. If it is, do not modify the pixel. And when setting the pixel, be sure to copy the alpha channel, not just RGB.

    Qt Code:
    1. Player::Player(QGraphicsPixmapItem *parent)
    2. {
    3. // Load the QImage directly from the resource. No need to create a QPixmap first
    4. QImage tmp( ":/Images/PlayerMoveDownStill.png", "PNG" );
    5.  
    6. for(int i=0;i<tmp.height();i++)
    7. {
    8. for(int k=0;k<tmp.width();k++)
    9. {
    10. QColor color( tmp.pixel( k, i ) ); // changed this from pixelColor() to ensure alpha is copied
    11. if ( color.alpha() != 0 ) // modify only the pixels with non-zero alpha
    12. {
    13. color.setRgb(color.red()*0.5,color.blue()*0.5,color.green()*0.5, color.alpha() );
    14. tmp.setPixelColor(k,i,color);
    15. }
    16. }
    17. }
    18.  
    19. // Now, convert the image to a pixmap and set it on the graphics object
    20. QPixmap t = QPixmap::fromImage( tmp );
    21. setPixmap( t );
    22. setPos(game->width()/2-this->boundingRect().width()/2,game->height()/2-this->boundingRect().height()/2);
    23. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  11. The following user says thank you to d_stranz for this useful post:

    FlyDoodle (20th September 2019)

  12. #9
    Join Date
    Sep 2019
    Posts
    16
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Darken QPixmap

    Thanks for the reply and showing me how to do it, unfortunately it didn't work but i tried couple of things and instead of:
    Qt Code:
    1. if ( color.alpha() != 0 )
    To copy to clipboard, switch view to plain text mode 

    I did :
    Qt Code:
    1. if ( tmp.pixel(k,i) != 0)
    To copy to clipboard, switch view to plain text mode 

    And now it works just fine :
    darkenwork.PNG

    Thank you for taking your time and helping me out with this, and everything so far, i really appreciate it !!! Now i can implement day and night into my game

  13. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,142
    Thanks
    235
    Thanked 661 Times in 651 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Darken QPixmap

    unfortunately it didn't work
    Strange. Maybe constructing a QColor from a QRgb value doesn't copy the alpha channel, so it gets set to 255 by default. Obviously QImage::pixel() returns the alpha channel along with the rgb channels, because you would not be able to compare it to zero otherwise. I learned something too.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Darken Widgets Behind a Dialog
    By johnsoga in forum Qt Programming
    Replies: 0
    Last Post: 23rd August 2010, 21:35
  2. How to not darken selected icons ?
    By Olivier Berten in forum Qt Programming
    Replies: 2
    Last Post: 22nd January 2010, 22:38
  3. Replies: 4
    Last Post: 28th August 2008, 13:13
  4. Replies: 1
    Last Post: 21st August 2008, 07:44
  5. Replies: 5
    Last Post: 9th April 2007, 14:26

Tags for this Thread

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.