How to convert multi page Magick++ Image to QImage?

1k views Asked by At

I convert Magick++ single page Image to QT image (QPixmap, actually, but could be QImage as well) with:

Blob my_blob_1;
Image img1;    
img1.magick("MNG"); // or PNG
img1.write(&my_blob_1);
const QByteArray imageData1((char*)(my_blob_1.data()),my_blob_1.length());
item1p.loadFromData(imageData1);
item1 = Scene->addPixmap(QPixmap(item1p));`

where:

QPixmap item1p;
QGraphicsScene *Scene;
QGraphicsPixmapItem *item1;`

My question is: how could I do that with multi page Image? Below, I have a multipage image in a vector, I manipulate it with STL algorithms, but I can not find a way to output it to QT Image. Magick++ writes it out to a single blob. I would need to write to separate blobs for each page. Do I, or is there other way? vector to QVector

Blob my_blob_111;
vector<Image> imageListmpp;
writeImages( imageListmpp.begin(), imageListmpp.end(), &my_blob_111 );
Image aaa;
aaa.read(my_blob_111);
aaa.write( "D:/test/aaa.pdf" );`

I welcome any suggestion.

Thanks!

1

There are 1 answers

0
Boris Dalstein On BEST ANSWER

Since you already have a vector of Magick Image, your mistake was to save them all as a unique Blob. Instead, convert them individually to a blob, and then to a QPixmap:

vector<Image> imageListmpp; // your input
QVector<QPixmap> pixmaps;   // your output (use std::vector if you prefer)
QVector<QGraphicsPixmapItem *> graphicsItems;  
for(int i=0; i<imageListmpp.size(); i++)
{
    // Get the individual image data
    Blob blob;
    imageListmpp[i].magick("MNG"); // or PNG
    imageListmpp[i].write(&blob);
    const QByteArray imageData((char*)(blob.data()),blob.length());

    // Convert the data to a QPixmap in the vector
    pixmaps << QPixmap();
    pixmaps.last().loadFromData(imageData);
    graphicsItems << Scene->addPixmap(pixmaps.last());
}