ImageMagick

最近使用一下 ImageMagick 处理图片,现在记录一下常用几个函数。
官方文档在这里

直接贴一个例子1
将彩色图片转换灰度图片保存。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
int main (int argc, char **argv)
{
if (argc < 2) {
printf("%s picture.\n", argv[0]);
exit(1);
}
const char *filename = argv[1];

MagickWandGenesis();

MagickWand *images = NewMagickWand();
if(!MagickReadImage(images, filename)) {
printf("E: %s read failed.\n", filename);
exit(2);
}

printf("Read: %s\n", MagickGetImageFilename(images));
int width = MagickGetImageWidth(images);
int height = MagickGetImageHeight(images);

size_t bloblen = width * height;
unsigned char *blob = malloc(bloblen);
if(!MagickExportImagePixels(images, 0, 0, width, height, "I", CharPixel, blob)) {
printf("E: read picture failed.\n");
exit(3);
}
// write
MagickWand *img = NewMagickWand();
PixelWand *pix = NewPixelWand();
if (!MagickNewImage(img, width, height, pix)) {
printf("E: new image failed.\n");
exit(4);
}
if (!MagickImportImagePixels(img, 0, 0, width, height, "I", CharPixel, blob)) {
printf("E: write picture failed.\n");
exit(5);
}
MagickWriteImage(img, "out.jpg");
printf("Write: out.jpg\n");

// clean
free(blob);
DestroyPixelWand(pix);
DestroyMagickWand(img);
DestroyMagickWand(images);
MagickWandTerminus();
exit(0);
}

注:
makefile 需要添加 ImageMagick 动态库路径
-I/usr/local/include/ImageMagick-6 -L/usr/local/include/ImageMagick-6/ -lMagickWand-6.Q16


ImageMagick
https://wishlily.github.io/article/code/2017/12/01/undefined/
作者
Wishlily
发布于
2017年12月1日
许可协议