c++调用ImageMagick进行图片格式转换

1.源码实现

#include 
#include 
#include 
#include 

using namespace std;

int main(int argc, char **argv)
{
    const char *input = "./1.png";
    const char *output = "./1.jpg";

    if(argc < 3)
    {
        return -1;
    }

    input = argv[1];
    output = argv[2];

    MagickWand *magick_wand = NULL;
    MagickBooleanType status;

    //初始化MagickWand
    MagickWandGenesis();

    //创建一个MagickWand实例
    magick_wand = NewMagickWand();

    //读取输入图片
    status = MagickReadImage(magick_wand, input);
    if(status == MagickFalse)
    {
        cout << "open image failed" << endl;

        magick_wand = DestroyMagickWand(magick_wand);

        MagickWandTerminus();

        return -1;
    }

    //保存图片为
    status = MagickWriteImages(magick_wand, output, MagickTrue);
    if(status == MagickFalse)
    {
        cout << "write image failed" << endl;

        magick_wand = DestroyMagickWand(magick_wand);

        MagickWandTerminus();

        return -1;
    }

    //销毁MagickWand实例
    magick_wand = DestroyMagickWand(magick_wand);

    //结束MagickWand
    MagickWandTerminus();

    return 0;
}

2.编译源码

$ g++ -o test test.cpp -I/usr/local/include/ImageMagick-6 -std=c++11 -L/usr/local/lib -lMagickWand-6.Q16 -Wl,-rpath=/usr/local/lib

3.运行及其结果

$ ./test 1.jpg 1.png

你可能感兴趣的:(c++调用ImageMagick进行图片格式转换)