Results 1 to 7 of 7

Thread: How to make a mask with QImage?

  1. #1
    Join Date
    Aug 2010
    Posts
    4
    Qt products
    Qt4

    Default How to make a mask with QImage?

    I want to make a mask image with QImage, I suppose the color of pixel (1, 1) is the brackground 's color and set that color to white, if the pixel's color(rgb) have big difference with the blackground's color then set the color to black.That's my code,but it doesn't work, when i set the mask, the picture doesnot show.
    Qt Code:
    1. QImage image;
    2. bool tt = image.load(fileName, 0);
    3. qDebug() << "Bool:" << tt;
    4. QColor white(Qt::white);
    5. QColor black(Qt::black);
    6.  
    7. int i =image.size().width();
    8. int j =image.size().height();
    9.  
    10. QImage ConvertImage (i, j, QImage::Format_RGB32);
    11. QColor maskColor = QColor::fromRgb (image.pixel(1, 1) );
    12.  
    13. int red = maskColor.red();
    14. int green = maskColor.green();
    15. int blue = maskColor.blue();
    16.  
    17. for(int x= 0; x<i; x++)
    18. {
    19. for(int y = 0; y<j; y++)
    20. {
    21. QColor color = QColor::fromRgb (image.pixel(x, y) );
    22.  
    23. if((abs(red - color.red()) +
    24. abs(green - color.green()) +
    25. abs(blue - color.blue()))/3 <10 )
    26. {
    27. ConvertImage.setPixel(x, y, white.rgb()) ;
    28. }
    29. else
    30. {
    31. ConvertImage.setPixel(x, y, black.rgb()) ;
    32. }
    33. }
    34. }
    35. QImage tmp = ConvertImage.convertToFormat(QImage::Format_Mono );
    36. tmp.save("test", "png");
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2010
    Posts
    26
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to make a mask with QImage?

    PNG does not support bilevel (1 bit per pixel) images, but you are converting to a bilevel image before saving.

    Try ConvertImage.save("test.png","png"); instead of the last 2 lines ?

  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: How to make a mask with QImage?

    Is QBitmap and QBitmap::fromImage() useful?

  4. #4
    Join Date
    Aug 2010
    Posts
    4
    Qt products
    Qt4

    Default Re: How to make a mask with QImage?

    Quote Originally Posted by koan View Post
    PNG does not support bilevel (1 bit per pixel) images, but you are converting to a bilevel image before saving.

    Try ConvertImage.save("test.png","png"); instead of the last 2 lines ?
    Actually if i change the format ,for example, bmp, it also doesn't work.

  5. #5
    Join Date
    Aug 2010
    Posts
    4
    Qt products
    Qt4

    Default Re: How to make a mask with QImage?

    Quote Originally Posted by ChrisW67 View Post
    I also have try this way, but it does not wrok
    Qt Code:
    1. bitImage = QBitmap::fromImage(ConvertImage);
    2. qDebug()<<bitImage.isQBitmap(); //print true
    3. ui.toolButton_2->setIcon(QPixmap::fromImage(image));
    4. ui.toolButton_2->setIconSize(QSize(32, 32));
    5. ui.toolButton_2->setMask( bitImage );
    6. ui.toolButton_2->setFixedSize(32, 32);
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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: How to make a mask with QImage?

    This works fine for me:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. QImage image("source.png");
    8.  
    9. QColor white(Qt::white);
    10. QColor black(Qt::black);
    11.  
    12. QImage ConvertImage (image.width(), image.height(), QImage::Format_RGB32);
    13. QColor maskColor = QColor::fromRgb (image.pixel(1, 1) );
    14.  
    15. int red = maskColor.red();
    16. int green = maskColor.green();
    17. int blue = maskColor.blue();
    18.  
    19. for(int x= 0; x<image.width(); x++) {
    20. for(int y = 0; y<image.height(); y++) {
    21. QColor color = QColor::fromRgb (image.pixel(x, y) );
    22.  
    23. if((abs(red - color.red()) +
    24. abs(green - color.green()) +
    25. abs(blue - color.blue()))/3 <10 ) {
    26. ConvertImage.setPixel(x, y, white.rgb()) ;
    27. }
    28. else {
    29. ConvertImage.setPixel(x, y, black.rgb()) ;
    30. }
    31. }
    32. }
    33. ConvertImage.save("mask.png", "png");
    34.  
    35. QBitmap b = QBitmap::fromImage(ConvertImage, Qt::MonoOnly);
    36.  
    37. QLabel l;
    38. l.setPixmap(QPixmap::fromImage(image));
    39. l.setMask(b);
    40. l.show();
    41. return app.exec();
    42. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Aug 2010
    Posts
    4
    Qt products
    Qt4

    Default Re: How to make a mask with QImage?

    Yes, used the label the mask works fine, but with button, it doesnot work, How strange!

Similar Threads

  1. Replies: 1
    Last Post: 12th May 2010, 13:12
  2. How it create a true alpha mask from a QImage?
    By nokkie in forum Qt Programming
    Replies: 1
    Last Post: 3rd May 2010, 21:07
  3. To make rounded Line edit( with out using mask )
    By ashishsaryar in forum Qt Programming
    Replies: 1
    Last Post: 23rd April 2010, 12:26
  4. how to make input mask and validator work together?
    By homerli in forum Qt Programming
    Replies: 11
    Last Post: 5th June 2009, 09:53
  5. Replies: 3
    Last Post: 15th March 2006, 11:44

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.