Results 1 to 6 of 6

Thread: Converting to transparent Image

  1. #1
    Join Date
    Aug 2008
    Posts
    134
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Converting to transparent Image

    hi,
    I am loading a QImage which has two colors(white and black). I want to convert this image in such a way that all the white becomes transparent. i.e. resultant image should be transparent image wherever white color appears in source image.
    Can anyone tell how can i accomplish this.

    Thank you in advance

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Converting to transparent Image

    You have to use :
    Qt Code:
    1. QImage QImage::createAlphaMask ( Qt::ImageConversionFlags flags = Qt::AutoColor ) const
    To copy to clipboard, switch view to plain text mode 

    And

    Qt Code:
    1. QImage QImage::createMaskFromColor ( QRgb color, Qt::MaskMode mode = Qt::MaskInColor ) const
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2008
    Posts
    134
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Converting to transparent Image

    Thank you for the reply. and i already used this function and function is not returning transparent one.

  4. #4
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Converting to transparent Image

    Can you show the code.

  5. #5
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Converting to transparent Image

    How about replacing each pixel with white color with a pixel with Qt::transparent.

    Qt Code:
    1. QImage img;
    2. img.load("test.jpg");
    3. for (int i = 0; i < img.height(); i++)
    4. {
    5. for (int j = 0; j < img.width(); j++)
    6. {
    7. if(img.pixel(j, i) == qRgba(255,255,255,255))
    8. img.setPixel(j, i, Qt::transparent);//Or you can use qRgba(0,0,0,0) instead for trans
    9. }
    10. }
    11. img.save("changed.png");//I dont remember that png can alpha channel or not.
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Converting to transparent Image

    If you use a 8-bit image, you can simply manipulate the color table (QImage::setColorTable()).

Similar Threads

  1. Create Transparent PNG Image
    By hvitual in forum Qt Programming
    Replies: 1
    Last Post: 2nd August 2009, 14:35
  2. How can I create a transparent image with QImage?
    By learning_qt in forum Qt Programming
    Replies: 1
    Last Post: 28th January 2009, 15:06
  3. can Qlabel display a series of image one by one ?
    By jirach_gag in forum Qt Tools
    Replies: 3
    Last Post: 11th August 2008, 15:36
  4. Display only PNG image on desktop
    By durbrak in forum Qt Programming
    Replies: 32
    Last Post: 15th March 2008, 21:55
  5. Finding marks on scanned image for alignment
    By caduel in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2007, 02:10

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.