简体   繁体   中英

How to resize and save gif file with Magick++ without animation loss

I have next code for resizing images:

Image image;
image.read( imagePath.toStdString() );
Geometry newSize = Geometry(m_newSize.width(), m_newSize.height());
newSize.aspect(true);
image.resize(newSize);
image.write(newImagePath.toStdString());

This code work fine for non-gif files. For gif files I loss animation: 在调整gif文件大小之前

辞职后

How to resize gif files without animation loss?

I found a solution! Thank you MofX for right direction!

   Geometry newSize = Geometry(m_newSize.width(), m_newSize.height());
   newSize.aspect(true); 
   list<Image> frames;
   readImages(&frames, imagePath.toStdString());
   for (auto& image : frames)
        image.resize(newSize);
   writeImages(frames.begin(), frames.end(), newImagePath.toStdString());

You have to use readImages and animateImages to read all frames of the animation into an image list and to add them to an animation.

Look at this example at http://www.imagemagick.org/Magick++/STL.html :

#include <list> 
#include <Magick++.h> 
using namespace std; 
using namespace Magick;

int main(int /*argc*/,char **/*argv*/) 
{ 
    list<Image> imageList; 
    readImages( &imageList, "test_image_anim.gif" );

    Image appended; 
    appendImages( &appended, imageList.begin(), imageList.end() ); 
    appended.write( "appended_image.miff" ); 
    return 0; 
}

The only problem is, that the documentation says, that animatImages works only under X11

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM