Petaflop
Petaflop

Reputation: 23

QT - Is there any class for combine few images into one?

I would like do some matrix of images, show preview in some widget and after all save it to - for example - jpg file. I know I could copy every image pixel per pixel into big one, but it wasn't efficient method I suppose... Are there any better solution?

Thanks in advice.

Upvotes: 2

Views: 5404

Answers (4)

lisandro
lisandro

Reputation: 309

I did the following:

// Load the images in order to have the sizes at hand.
QPixmap firstPixmap(":/images/first.png");
QPixmap secondPixmap(":/images/second.png");
const int gap = 25;

// Create an image with alpha channel large enough to contain them both and the gap between them.
QImage image(firstPixmap.width() + gap + secondPixmap.width(), firstPixmap.height(), QImage::Format_ARGB32_Premultiplied);
// I happen to need it transparent.
image.fill(QColor(Qt::transparent));

// Paint everything in a pixmap.
QPixmap pixmap = QPixmap::fromImage(image);
QPainter paint(&pixmap);
paint.drawPixmap(0, 0, firstPixmap);
paint.drawPixmap(firstPixmap.width() + gap, 0, secondPixmap);

// Use it.
QIcon icon(pixmap);
[...]

Upvotes: 0

Ton van den Heuvel
Ton van den Heuvel

Reputation: 10528

Instead of copying individual pixels, I would just directly draw each individual image on a QPixmap large enough to hold all images combined. The collage can then be generated by drawing each individual image on the collage as follows (untested code):

QList<QPixmap> images;
QPixmap collage;

// Make sure to resize collage to be able to fit all images.
...

for (QList<QPixmap>::const_iterator it = images.begin(); it != images.end(); ++it)
{
    int x = 0;
    int y = 0;

    // Calculate x & y coordinates for the current image in the collage.
    ...
    QPainter painter(&collage);
    painter.drawPixmap(
            QRectF(x, y, (*it).width(), (*it).height()), *it,
            QRectF(0, 0, (*it).width(), (*it).height()));
}

Note that QImage can be used as well instead of QPixmap. QPixmap is optimized for on screen display though. See the Qt documentation for more details.

Upvotes: 7

grimblegrumble
grimblegrumble

Reputation: 205

The code above is not working for me, i can not figure out why.

What i needed was a collage like:
PicA | PicB | Pic...| ... |
I ve found something similar with QImage and this code works.
(tested code):

const int S_iconSize = 80;     //The pictures are all quadratic.
QList<const QPixmap*> t_images;//list with all the Pictures, PicA, PicB, Pic...
QImage resultImage(S_iconSize*t_images.size(), S_iconSize, QImage::Format_ARGB32_Premultiplied);
QPainter painter;

painter.begin(&resultImage);
for(int i=0; i < t_images.size(); ++i)
{
    painter.drawImage(S_iconSize*i, 0, t_images.at(i)->toImage(), 0, 0, S_iconSize, S_iconSize, Qt::AutoColor);
}
painter.end();

QPixmap resultingCollagePixmap = QPixmap::fromImage(resultImage);

I know it is ugly cause the QImage is converted to QPixmap and vice versa, but it works. So if someone has an idea how to make the code above (from Ton van den Heuvel) run i would be glad. (Maybe it is just a missing QPainter???)

Regards

Upvotes: 0

thorsten m&#252;ller
thorsten m&#252;ller

Reputation: 5651

No, you don't want to do this pixal by pixel. QImage is a QPaintDevice. So you can load them, render them into each other and save them in several formats as you like. And of course display them on the screen.

Upvotes: 1

Related Questions