I'm trying to add text to an image using ImageMagick. I see multiple examples using draw, label, caption, annotate et al. What is the difference between the above? I am able to test the results of the above commands with CLI, however am facing trouble when trying to run by java using IM4java. Any help with java code snippets will be useful.
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
And also a narrow, fixed size:
convert -background black -fill white -gravity center -size 100x100 label:'Stack Overflow' text.png
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
And also a large point-size without a canvas size:
convert -background black -fill white -gravity center -pointsize 64 label:'Stack Overflow' text.png
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
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.
来源:https://stackoverflow.com/questions/55469545/difference-between-caption-draw-annotate-label-while-adding-text-to-imagemagi