PDA

View Full Version : Problem with change image format



danilodsp
6th September 2011, 07:08
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.


QImage imageF = QImage(fn);
imageF = imageF.convertToFormat(QImage::Format_Indexed8, Qt::ThresholdDither|Qt::AutoColor);
ui->label->setPixmap(QPixmap::fromImage(imageF));

What the possible problem?

high_flyer
7th September 2011, 11:52
What is 'fn'?
Are you sure that imageF is a valid Image?

If you do the following:


QImage imageF = QImage(fn);
QImage::Format format = imageF.format();
imageF = imageF.convertToFormat(QImage::Format_Indexed8, Qt::ThresholdDither|Qt::AutoColor);
ui->label->setPixmap(QPixmap::fromImage(imageF));


What do you get in 'format'?

danilodsp
8th September 2011, 04:16
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.

ChrisW67
8th September 2011, 08:04
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.