Charles Burns
Charles Burns

Reputation: 10602

Qt: QImage always saves transparent color as black

How do I save a file with transparency to a JPEG file without Qt making the transparent color black? I know JPEG doesn't support alpha, and the black is probably just a default "0" value for alpha, but black is a horrible default color.

It seems like this should be a simple operation, but all of the mask and alpha functions I've tried are ignored when saving as JPEG.

For example:

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

I've tried filling the image with white before saving as a JPEG, converting the image to ARGB32 (with 8-bit alpha channel) before saving, and even tried ridiculously slow stuff 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.

Upvotes: 7

Views: 11829

Answers (3)

Andres
Andres

Reputation: 1

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

Upvotes: -2

Lukáš Lalinský
Lukáš Lalinský

Reputation: 41316

I'd try something like this (i.e., load the image, create another image of the same size, paint the background, paint the 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");

Upvotes: 11

Martin Beckett
Martin Beckett

Reputation: 96167

Jpeg doesn't support transparency

Upvotes: -2

Related Questions