「ImageMagick」- 常用命令及常见问题

ImageMagick,用于读取、写入、操作各种图像格式的图像。

安装程序

从发行版的源中安装

apt-get install $(apt-cache search imagemagick | grep -iE '^ImageMagick' | awk '{printf"%s ", $1}')

使用源码安装
参考:BLFS/ImageMagick-6.9.5-8: http://www.linuxfromscratch.org/blfs/view/7.10/general/imagemagick.html

安装的可执行程序

animate | animates a sequence of images.
compare | compares an image to a reconstructed image.
conjure | processes a MSL script to create an image.
display | displays an image.
identify | 描述出一张图片的格式、特征及其他的元信息。
import | captures an X window.
Magick{,++,Core,Wand}-config | show information about the installed versions of the ImageMagick libraries.
mogrify | transforms an image.
montage | composites various images into a new image.
stream | streams one or more pixel components of an image or portion of the image to your choice of storage formats.
Wand-config | shows the options required to use the Wand library.
Image::Magick | allows the reading, manipulation and writing of a large number of image file formats using the ImageMagick library. Run make in the PerlMagick/demo directory of the package source tree after the package is installed to see a nice demo of the module’s capabilities.

convert

该命令用于将文件从一个格式转化为另外一个格式。

场景:为图片添加边框

https://legacy.imagemagick.org/discourse-server/viewtopic.php?t=31805 
https://askubuntu.com/questions/819482/how-to-add-a-border-using-imagemagick 
https://stackoverflow.com/questions/4481573/how-to-override-windows-convert-command-by-imagemagicks-one

convert -bordercolor grey -border 2 input.png output.png                        # 直接添加

convert -shave 2 -bordercolor grey -border 2 input.png output.png               # 在内部添加边框

# 在 Windows ImageMagick-7.1.0 中,需要使用 magick convert ... 形式执行

场景:使图片中指定的颜色变为透明(-transparent color):

convert test.png -transparent white transparent.png

场景:修改宽度,并保持高度比例(Imagemagick: Convert to fixed width, proportional height
使用 -resize 100x 选项

composite

该程序是 ImageMagick(1) 套件的一部分,将各种图像合成到给定的基础图像中,即用它将一张图叠放在另外一张图上。

有关composite命令的详细信息查看:
1)在 Debian 中,参考 imagemagick-6-doc 软件包;
2)http://www.imagemagick.org/script/composite.php

常见问题汇总

attempt to perform an operation not allowed by the security policy `PDF’

ImageMagick security policy ‘PDF’ blocking conversion – Stack Overflow

当执行 convert 命令是,产生如下错误:

# convert -density 300 -depth 8 -quality 90 input.pdf output.png
convert: attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/408.
convert: no images defined `output.png' @ error/convert.c/ConvertImageCommand/3288.

这是因为 Ghostscript 的 Bug 所以才添加该策略。

通过修改 /etc/ImageMagick-7/policy.xml 文件以解决,添加如下配置行:

...
  <policy domain="coder" rights="read | write" pattern="PDF" />
</policymap>

参考文献

ImageMagick Homepage
BLFS/ImageMagick-6.9.5-8