向ImageMagick添加文本时标题,绘制,注释,标签之间的区别

May*_* M. 3 text annotate imagemagick caption imagemagick-convert

我正在尝试使用ImageMagick将文本添加到图像。我看到了多个使用draw,label,caption,annotate等的示例。两者之间有什么区别?我可以使用CLI测试上述命令的结果,但是在尝试使用IM4java由java运行时遇到了麻烦。Java代码段的任何帮助将非常有用。

Mar*_*ell 5

Here is how I perceive it - it is quite opinionated and others are welcome to edit and add their insights.


label: Like other operators that contain a colon (:), e.g. gradient:, xc:, logo:, the label: operator generates its own canvas. That means you don't draw/type text onto an existing image, but rather you just draw/type your text and it creates a background for that text to sit on.

If you specify -size beforehand, it will create a canvas that size and put the text on it at the biggest pointsize that fits. So, let's try a wide, fixed size:

convert -background black -fill white -gravity center -size 800x100 label:'Stack Overflow' text.png
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

And also a narrow, fixed size:

convert -background black -fill white -gravity center -size 100x100 label:'Stack Overflow' text.png
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

If you don't specify -size beforehand, it will create the text at the point-size you ask for and put it on a suitably sized canvas. So, let's try a small pointsize without a canvas size:

convert -background black -fill white -gravity center -pointsize 16 label:'Stack Overflow' text.png
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

And also a large point-size without a canvas size:

convert -background black -fill white -gravity center -pointsize 64 label:'Stack Overflow' text.png
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

You can also specify just the width but no height, e.g. with -size 200x, or just the height but not the width, e.g. -size x50, and it will use the largest font it can but be constrained in the dimension you specify.

The following should give you an idea of which attributes of the text you can affect:

convert -background black -fill yellow -strokewidth 2 -stroke magenta \
    -undercolor blue -size 400x100 -gravity center -font 'AppleChancery' label:'Stack Overflow' text.png
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


caption: is like label: but it also does word wrapping so it will spread a long sentence across multiple lines for you all on its own.


pango: is a reasonably sophisticated markup language resembling HTML, that allows you to change fonts, colours, bold, italic, subscripts, superscripts and other text features mid-sentence.


-draw "text 10,10 'Your message'" is somewhat deprecated but it allows you to draw onto an existing image at a specific location, such as the 10,10 shown above. Note that it has no colon (:) so you need to already have an image/canvas for it to draw into.


-annotate really supersedes -draw. Like -draw, you need to have a canvas/image already on which to draw, but then it allows you to position, shear and rotate your text more readily than with -draw.


Anthony Thyssen provides an excellent discussion of all of these things, and more here.