=====================================================

ImageMagick的使用和开发的文章:

小试ImageMagik——使用篇

小试ImageMagik——开发篇

=====================================================

本文介绍使用ImageMagick开发程序的方法。ImageMagick安装之后就可以支持C/C++程序的开发,提供了3种接口。在这里首先介绍一下ImageMagick的3种接口。

MagickCore:

底层的C语言接口。较复杂,但是可以修改很多参数,只适合高端用户使用。

MagickWand:

推荐的C语言接口。相比于MagickCore接口,简单很多。适合普通用户使用。

Magick++:

提供面向对象的C++接口。

下面回顾一下ImageMagick安装后目录:

其中几个和开发有关的文件:

Lib文件夹:开发需要使用的静态库文件。包含4个库,前3个对应ImageMagick的3个接口:

CORE_RL_magick_.lib; CORE_RL_Magick++_.lib; CORE_RL_wand_.lib; X11.lib;

Include文件夹:开发需要使用的头文件。包含3个文件夹,对应ImageMagick的3个接口:

magick; Magick++; wand;

*.dll:开发和使用过程中需要使用的动态链接库文件。

在开发中我们需要3种文件:头文件(*.h),静态库文件(*.lib),动态库文件(*.dll)。因此我们在VC中新建一个工程,然后将Lib文件夹,Include文件夹,以及dll拷贝到工程目录下,并且配置一下头文件和静态库的路径,就可以了。

下面分别给出ImageMagick的3种接口对应的例子。

MagickCore(底层的C语言接口。较复杂,但是可以修改很多参数,只适合高端用户使用)

功能:读取文件,创建缩略图,并保存成文件。

/*
 * 雷霄骅
 * leixiaohua1020@126.com
 * http://blog.csdn.net/leixiaohua1020
 * 中国传媒大学/数字电视技术
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <magick/MagickCore.h>

int main(int argc,char **argv)
{
  ExceptionInfo
    *exception;

  Image
    *image,
    *images,
    *resize_image,
    *thumbnails;

  ImageInfo
    *image_info;

  if (argc != 3)
    {
      (void) fprintf(stdout,"Usage: %s image thumbnail\n",argv[0]);
      exit(0);
    }
  /*
    Initialize the image info structure and read an image.
  */
  MagickCoreGenesis(*argv,MagickTrue);
  exception=AcquireExceptionInfo();
  image_info=CloneImageInfo((ImageInfo *) NULL);
  (void) strcpy(image_info->filename,argv[1]);
  images=ReadImage(image_info,exception);
  if (exception->severity != UndefinedException)
    CatchException(exception);
  if (images == (Image *) NULL)
    exit(1);
  /*
    Convert the image to a thumbnail.
  */
  thumbnails=NewImageList();
  while ((image=RemoveFirstImageFromList(&images)) != (Image *) NULL)
  {
    resize_image=ResizeImage(image,106,80,LanczosFilter,1.0,exception);
    if (resize_image == (Image *) NULL)
      MagickError(exception->severity,exception->reason,exception->description);
    (void) AppendImageToList(&thumbnails,resize_image);
    DestroyImage(image);
  }
  /*
    Write the image thumbnail.
  */
  (void) strcpy(thumbnails->filename,argv[2]);
  WriteImage(image_info,thumbnails);
  /*
    Destroy the image thumbnail and exit.
  */
  thumbnails=DestroyImageList(thumbnails);
  image_info=DestroyImageInfo(image_info);
  exception=DestroyExceptionInfo(exception);
  MagickCoreTerminus();
  return(0);
}

MagickWand(推荐的C语言接口。相比于MagickCore接口,简单很多。适合普通用户使用)

功能: 读取文件,创建缩略图,并保存成文件。

/*
 * 雷霄骅
 * leixiaohua1020@126.com
 * http://blog.csdn.net/leixiaohua1020
 * 中国传媒大学/数字电视技术
 */
#include <stdio.h>
#include <stdlib.h>
#include <wand/MagickWand.h>

int main(int argc,char **argv)
{

  MagickBooleanType status;

  MagickWand *magick_wand;

  if (argc != 3)
    {
      (void) fprintf(stdout,"Usage: %s image thumbnail\n",argv[0]);
      exit(0);
    }
  /*
    Read an image.
  */
  MagickWandGenesis();
  magick_wand=NewMagickWand();
  status=MagickReadImage(magick_wand,argv[1]);
  /*
    Turn the images into a thumbnail sequence.
  */
  MagickResetIterator(magick_wand);
  while (MagickNextImage(magick_wand) != MagickFalse)
    MagickResizeImage(magick_wand,106,80,LanczosFilter,1.0);
  /*
    Write the image then destroy it.
  */
  status=MagickWriteImages(magick_wand,argv[2],MagickTrue);

  magick_wand=DestroyMagickWand(magick_wand);
  MagickWandTerminus();
  return(0);
}

Magick++(提供面向对象的C++接口)

/*
 * 雷霄骅
 * leixiaohua1020@126.com
 * http://blog.csdn.net/leixiaohua1020
 * 中国传媒大学/数字电视技术
 */
//创建Image对象,
// create a blank image canvas with 640x480 size and 'white' color as background:
 Image blank_image( Geometry(640, 480), Color(MaxRGB, MaxRGB, MaxRGB, 0));
 // or also, by using the automatic C++ type conversions for the arguments:
 Image blank_image("640x480", "white");

 // create an image from URL
 Image url_image("http://www.serverName.com/image.gif");
 Image local_file_image("my_image.gif");   // here the URL points to the local filesystem
//获取/设置属性
// Canvas geometry
// returns an unsigned int representing the my_image width
unsigned int Image::columns();
// returns an unsigned int representing the my_image heigth
unsigned int Image::rows();
// sets the my_image format; the format string can be "GIF", etc
void Image::magick("png");
// returns a string value representing the image format (e.g. “GIF”, “JPEG”, etc)
string Image::magick();
//读取/保存图像文件
// Reading the contents of a disk file into an image object can be performed
Image my_image();  // create an *empty* image using the default Image constructor
// read a GIF image file from disk; the image format is automatically set to GIF
my_image.read("aGIFImageFile.gif");
// Writing an Image object to a disk file. set the "format" attribute of my_image to PNG
my_image.magick("png");
// write to disk an image file
my_image.write("file_name_explicit_extension.gif");

MagickWand一般情况下属于使用比较普遍的,下面记录两个MagickWand开发的例子。

更多的例子可以参考:http://members.shaw.ca/el.supremo/MagickWand/

功能:将图像的宽高变成源图像的50%

/*
 * 雷霄骅
 * leixiaohua1020@126.com
 * http://blog.csdn.net/leixiaohua1020
 * 中国传媒大学/数字电视技术
 */
#include <windows.h>
#include <wand/magick_wand.h>

void test_wand(void)
{
         MagickWand *m_wand = NULL;

         int width,height;

         MagickWandGenesis();

         m_wand = NewMagickWand();
         // Read the image - all you need to do is change "logo:" to some other
         // filename to have this resize and, if necessary, convert a different file
         MagickReadImage(m_wand,"logo:");

         // Get the image's width and height
         width = MagickGetImageWidth(m_wand);
         height = MagickGetImageHeight(m_wand);

         // Cut them in half but make sure they don't underflow
         if((width /= 2) < 1)width = 1;
         if((height /= 2) < 1)height = 1;

         // Resize the image using the Lanczos filter
         // The blur factor is a "double", where > 1 is blurry, < 1 is sharp
         // I haven't figured out how you would change the blur parameter of MagickResizeImage
         // on the command line so I have set it to its default of one.
         MagickResizeImage(m_wand,width,height,LanczosFilter,1);

         // Set the compression quality to 95 (high quality = low compression)
         MagickSetImageCompressionQuality(m_wand,95);

         /* Write the new image */
         MagickWriteImage(m_wand,"logo_resize.jpg");

         /* Clean up */
         if(m_wand)m_wand = DestroyMagickWand(m_wand);

         MagickWandTerminus();
}

功能:在图像的周围加上边框

/*
 * 雷霄骅
 * leixiaohua1020@126.com
 * http://blog.csdn.net/leixiaohua1020
 * 中国传媒大学/数字电视技术
 */
#include <windows.h>
#include <wand/magick_wand.h>

void test_wand(void)
{
         MagickWand *m_wand = NULL;
         PixelWand *p_wand;
         int w,h;

         MagickWandGenesis();

         /* Create a wand */
         m_wand = NewMagickWand();
         p_wand = NewPixelWand();

         // Change this to whatever colour you like - e.g. "none"
         PixelSetColor(p_wand, "blue");
         /* Read the input image */
         MagickReadImage(m_wand,"logo:");
         w = MagickGetImageWidth(m_wand);
         h = MagickGetImageHeight(m_wand);
         MagickSetImageBackgroundColor(m_wand,p_wand);
         // This centres the original image on the new canvas.
         // Note that the extent's offset is relative to the
         // top left corner of the *original* image, so adding an extent
         // around it means that the offset will be negative
         MagickExtentImage(m_wand,1024,768,-(1024-w)/2,-(768-h)/2);
         MagickWriteImage(m_wand,"logo_extent.jpg");

         /* Tidy up */
         m_wand = DestroyMagickWand(m_wand);
         p_wand = DestroyPixelWand(p_wand);
         MagickWandTerminus();
}

补充:详细的教程可以从ImageMagick的官网(http://www.imagemagick.org/)左侧的目录中查看。在Program Interfaces里面有几种接口的详细开发说明。

小试ImageMagik——开发篇的更多相关文章

  1. 小试ImageMagik——使用篇

    ===================================================== ImageMagick的使用和开发的文章: 小试ImageMagik--使用篇 小试Imag ...

  2. 华清远见金牌讲师名家大讲堂Android开发篇成功举办

    2014年3月5日.12日华清远见金牌讲师名家大讲堂(以下简称名家大讲堂)在线讲座全新升级开讲,至此拉开了新一年名家大讲堂的序幕! 华清远见名家大讲堂作为业内颇具影响力的公益免 费线上课程,自2009 ...

  3. 开年钜献:华清远见金牌讲师名家大讲堂(Android开发篇)

        华清远见作为嵌入式培训领导品牌,嵌入式就业课程已成为业内公认的专业人才培养体系!华清远见致力于让更多嵌入式技术爱好者及在校大学生获得一线嵌入式系统开发关键技术应用的经验,于2009年始开办名家 ...

  4. E8.Net工作流平台开发篇

    E8.Net开发篇(一)   E8.Net开发框架有哪些源程序模型? E8.Net开发框架为开发企业流程应用系统提供了最佳实践的开发架构.范例及源代码,包括待办事项的组织.流程启动模型.处理模型.母版 ...

  5. linux一句话问答(网络无关篇+网络相关篇+程序开发篇+经典图书)

    一句话问答(网络无关篇+网络相关篇+程序开发篇+经典图书) --------------------------目录-网络无关篇-目录-------------------------- 0001 修 ...

  6. .NET Core实战项目之CMS 第十一章 开发篇-数据库生成及实体代码生成器开发

    上篇给大家从零开始搭建了一个我们的ASP.NET Core CMS系统的开发框架,具体为什么那样设计我也已经在第十篇文章中进行了说明.不过文章发布后很多人都说了这样的分层不是很合理,什么数据库实体应该 ...

  7. Mac 配置教程-开发篇

    将 Mac 日常使用的软件和开发软件区分开,将之前写的 Mac 配置的文章分成了两篇: Mac 配置教程-日常篇 Mac 配置教程-开发篇 图床 iPic 设置快捷键 Command+Shift+u ...

  8. Hyperledger fabric-SDK-GO客户端开发篇(六)

    Hyperledger fabric-SDK-GO客户端开发篇(六) Fabric-SDK-GO是提供的Go语言开发包,应用程序可以利用Fabric-SDK-GO与fabric网络进行交互并访问链码. ...

  9. nginx模块开发篇 (阿里著作)

    背景介绍 nginx历史 使用简介 nginx特点介绍 nginx平台初探(100%) 初探nginx架构(100%) nginx基础概念(100%) connection request 基本数据结 ...

随机推荐

  1. Intellij Error:Cannot build Artifact 'XXX:war exploded' because it is included into a circular dependency

    外网的流程是这样的. 1: 2: 3: 4: 基本按这个来就好了 如果到了build artfact哪里按钮是灰色 就要手动建了 https://jingyan.baidu.com/album/0a5 ...

  2. Python小代码_3_购物车

    product_list = [ ('MacBook', 9000), ('kindle', 500), ('tesla', 900000), ('book', 100), ('bike', 2000 ...

  3. 吴恩达深度学习第4课第3周编程作业 + PIL + Python3 + Anaconda环境 + Ubuntu + 导入PIL报错的解决

    问题描述: 做吴恩达深度学习第4课第3周编程作业时导入PIL包报错. 我的环境: 已经安装了Tensorflow GPU 版本 Python3 Anaconda 解决办法: 安装pillow模块,而不 ...

  4. Node.js系列文章:利用console输出日志文件

    通常我们在写Node.js程序时,都习惯使用console.log打印日志信息,但这也仅限于控制台输出,有时候我们需要将信息输出到日志文件中,实际上利用console也可以达到这个目的的,今天就来简单 ...

  5. 开源Spring解决方案--lm.solution

    Github 项目地址: https://github.com/liumeng0403/lm.solution 一.说明 1.本项目未按java项目传统命名方式命名项目名,包名 如:org.xxxx. ...

  6. 解决Error: ENOENT: no such file or directory, scandir 'D:\IdeaWork\code-front-jet\node_modules\.npminstall\node-sass\3.7.0\node-sass\vendor'

    在使用npm安装node-sass的时候,可能会出现如下的报错: Error: ENOENT: no such file or directory, scandir 'D:\IdeaWork\code ...

  7. JAVA不可变类(immutable)机制与String的不可变性

    一.不可变类简介 不可变类:所谓的不可变类是指这个类的实例一旦创建完成后,就不能改变其成员变量值.如JDK内部自带的很多不可变类:Interger.Long和String等. 可变类:相对于不可变类, ...

  8. OAuth 2 深入介绍

    1. 前言 2. OAuth2 角色 2.1 资源所有者(Resource Owner) 2.2 资源/授权服务器(Resource/Authorization Server) 2.3 客户端(Cli ...

  9. Jedis分片Sentinel连接池实验

    Jedis分片Sentinel连接池实验 1.起因 众所周知,Redis官方HA工具Sentinel已经问世很久了,但令人费解的是,Jedis官方却迟迟没有更新它的连接池.到目前Maven库中最新的2 ...

  10. 六星经典CSAPP-笔记(11)网络编程

    六星经典CSAPP-笔记(11)网络编程 参照<深入理解计算机系统>简单学习了下Unix/Linux的网络编程基础知识,进一步深入学习Linux网络编程和TCP/IP协议还得参考Steve ...