Results 1 to 4 of 4

Thread: Problem with change image format

  1. #1
    Join Date
    Sep 2009
    Posts
    31
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem with change image format

    Hi friends,

    I can not change the format of an image.
    I want the image has only 8 bits, only 256 colors, but any changes will not work.
    I tried setNumColors(), convertToFormat(), and not work.

    Qt Code:
    1. QImage imageF = QImage(fn);
    2. imageF = imageF.convertToFormat(QImage::Format_Indexed8, Qt::ThresholdDither|Qt::AutoColor);
    3. ui->label->setPixmap(QPixmap::fromImage(imageF));
    To copy to clipboard, switch view to plain text mode 

    What the possible problem?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Problem with change image format

    What is 'fn'?
    Are you sure that imageF is a valid Image?

    If you do the following:
    Qt Code:
    1. QImage imageF = QImage(fn);
    2. QImage::Format format = imageF.format();
    3. imageF = imageF.convertToFormat(QImage::Format_Indexed8, Qt::ThresholdDither|Qt::AutoColor);
    4. ui->label->setPixmap(QPixmap::fromImage(imageF));
    To copy to clipboard, switch view to plain text mode 

    What do you get in 'format'?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2009
    Posts
    31
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem with change image format

    fn is the filename.
    imageF is ok.

    The conversion is ok, the format is 3 (Format_Indexed8).
    But I want an image to grayscale representation of 8 bits (256 colors).

    I'm doing "valuePixel = (qRed(imageF.pixel(1,1)) + qGreen(imageF.pixel(1,1)) + qBlue(imageF.pixel(1,1)))/3;" to get the value of each pixel.
    But I think it's possible before turning the image into grayscale 8-bit.

  4. #4
    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: Problem with change image format

    Quote Originally Posted by danilodsp View Post
    But I want an image to grayscale representation of 8 bits (256 colors).
    Try building a 256 element grey colour table and then call the variant of QImage::convertToFormat() that takes a colour table and build an indexed image that way.

    If you want to do it manually then look at qGray(). Your simple average of R, G, and B values is not a good reflection of the perceived brightness of colours.
    Last edited by ChrisW67; 8th September 2011 at 07:05. Reason: Fixed fat-handed typing

  5. The following user says thank you to ChrisW67 for this useful post:

    danilodsp (10th September 2011)

Similar Threads

  1. How to change current line format in QTextEdit?
    By FinderCheng in forum Qt Programming
    Replies: 5
    Last Post: 25th March 2011, 09:41
  2. change label format
    By kpr in forum Qwt
    Replies: 0
    Last Post: 15th February 2011, 09:44
  3. Replies: 0
    Last Post: 28th November 2010, 21:29
  4. TIFF image Format
    By dima in forum Qt Programming
    Replies: 7
    Last Post: 13th September 2010, 13:42
  5. How can change image format?
    By electronicboy in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2009, 09:44

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
  •  
Qt is a trademark of The Qt Company.