PDA

View Full Version : Greyscale png with transparency



Chops211
9th May 2018, 16:52
I have an image I create that is normalized to 8-bit (0->255) stored as quint8*. I pass this to QImage and it works well. Is there anyway to set the black pixels to transparent without using libpng to set the tRNS?

Santosh Reddy
10th May 2018, 10:10
Convert the QImage to one of the formats which support alpha channel (example QImage::Format_ARGB32) , then set the black color pixels with alpha color.

Read about
QImage::convertToFormat();
QImage:: pixelColor();
QImage::setPixelColor();

Chops211
10th May 2018, 15:27
So after I make the QImage in greyscale, convert to ARGB, set the pixels to transparent, and then back to greyscale? There isn't a more efficient method?

I have that working properly but only when the image is saved as ARGB. I want to keep it in Format_Grayscale8 when creating the PNG. No reason to use 4x the memory.

Added after 8 minutes:

If anyone wants the solution, none of this is necessary. If you want a value to be transparent in an 8-bit grayscale QImage that will be saved to a PNG, there is not anyway to do that inheritely in Qt it seems. You need to manually add the tRNS header and value to the PNG header.