PDA

View Full Version : HELP PLEASE- Superimpose two images in a QPixmap



NoQuiteFeynman
30th July 2013, 14:54
Hello

I wonder if you can assist. I'm running Qt Creator 2.7.0 based on Qt 5.0.2 using a Windows 7 Enterprise Intel Core 2 Duo E7200.

I'm a complete Newbie so please forgive my somewhat trivial query.

My aim is to superpose two .jpg images into a QPixmap.

However, I have followed others' suggestions (on this board) for an identical problem, and using their suggestions, I have tried the following code:






//.................................................. .................................................. ................................

// LOAD IMAGE
if (!fileName.isEmpty()) {
QImage image(fileName);
if (image.isNull()) {
QMessageBox::information(this, tr("Mammogram Viewer"),
tr("Cannot load %1.").arg(fileName));
return;
}

//.................................................. .................................................. ................................
QImage BottomMammogram(fileName);
BottomMammogram= BottomMammogram.convertToFormat(QImage::Format_ARG B32);
image.fill(qRgba(0,0,0,0));

QImage TopMammogram("C:/TopMammogram");
TopMammogram= TopMammogram.convertToFormat(QImage::Format_ARGB32 );
image.fill(qRgba(0,0,0,0));

painter1 = new QPainter;
img = new QPixmap(500, 500);

painter1->begin(img);
painter1->fillRect(img->rect(), Qt::transparent);
painter1->drawImage(0, 0, BottomMammogram); // puts BottomMammogram image into img
painter1->drawImage(50, 50, TopMammogram); // puts TopMammogram image into img
painter1->end();

imageLabel->setPixmap(*img);
imageLabel->show();



I have had no success with the above code, since the top Picture is completely opaque.

Any help would be gratefully appreciated.

Thank you in advance.


NotQuiteFeynman

wysota
30th July 2013, 15:19
If you are drawing an opaque image over another opaque image, why do you expect to receive something else than an opaque image drawn over another image?

NoQuiteFeynman
30th July 2013, 18:18
Hello

Thank you for the reply - it's much appreciated.

Sorry if I'm being daft, but doesn't 255 specify no opacity? Please see:

http://www.qtcentre.org/threads/13075-QImage-and-Qt-transparent

Your further advice would be much appreciated.

Kindest regards

wysota
30th July 2013, 18:22
Hello

Thank you for the reply - it's much appreciated.

Sorry if I'm being daft, but doesn't 255 specify no opacity? Please see:

http://www.qtcentre.org/threads/13075-QImage-and-Qt-transparent


I don't see any "255" in your code. Could you elaborate what you mean?

NoQuiteFeynman
30th July 2013, 21:38
Hi

Thanks for the continuing responses.

I thought you meant that I need to use image.fill(qRgba(0,0,0,255)); for transparency! Other advice on the board says I need to use image.fill(qRgba(0,0,0,0)); for transparency.

Should I use something else for transparency?

Please advise.

Kindest regards

wysota
30th July 2013, 21:42
Should I use something else for transparency?

A transparent image that you render on the pixmap (be it transparent or not). I don't know what sizes your jpeg images have but assuming their size is somewhere close to 500x500, they will most probably cover the whole area of the pixmap so it doesn't matter if you "fill the pixmap with transparency" or not if you then draw a non-transparent image over it.

ChrisW67
30th July 2013, 23:43
Zero in the alpha channel makes a pixel totally transparent, and 1.0 (255 when expressed as 8 bit int) makes it totally opaque.

I gather you want a partly transparent top image to be drawn over the bottom image.



QImage top("top.jpg");
QImage bot("bottom.jpg");
// both images are opaque because JPEG has no alpha channel

QPixmap combined(bot.size());
QPainter p(&combined);
p.drawImage(QPoint(0, 0), bot); // drawn as-is
p.setOpacity(0.2);
p.drawImage(QPoint(0, 0), top); // drawn at 20% opacity
p.end();

NoQuiteFeynman
30th August 2013, 12:08
Hi

This works, thank you very much for your help ChrisW67 and Wysota, it's very much appreciated!

All the best.

Kindest regards