目录

ImageMagick

目录

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

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

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