Qt: QImage always keeps a transparent color as black

How to save transparency file to JPEG file without Qt, making transparent color black? I know that JPEG does not support alpha, and black is probably just the default value โ€œ0โ€ for alpha, but black is a terrible default color.

It seems like it should be a simple operation, but all the masks and alpha functions that I tried are ignored when saving in JPEG format.

For instance:

image->load("someFile.png"); // Has transparent background or alpha channel image->save("somefile.jpg", "JPG"); // Transparent color is black 

I tried to fill the image with white before saving in JPEG format, converting the image to ARGB32 (with an 8-bit alpha channel) before saving and even trying ridiculously slow things like:

 QImage image2 = image1->convertToFormat(QImage::Format_ARGB32); image2.setAlphaChannel(image1->alphaChannel()); image2.save(fileURI, "JPG", this->jpgQuality; // Still black! See: http://67.207.149.83/qt_black_transparent.png for a visual. 
+4
source share
3 answers

I would try something like this (i.e. upload an image, create another image of the same size, draw a background, draw an image):

 QImage image1("someFile.png"); QImage image2(image1.size()); image2.fill(QColor(Qt::white).rgb()); QPainter painter(&image2); painter.drawImage(0, 0, image1); image2.save("somefile.jpg", "JPG"); 
+9
source

Jpeg does not support transparency

-2
source

True, if you want to use the Alpha Chanel (Transparent), you must save the imge in * .png * .bmp formats

-2
source

All Articles