What is text syntax in ImageMagick

2020-05-06 01:23发布

问题:

I use following code convert text to image

convert -size 1000x100 xc:transparent -pointsize 30 -draw "text 0,30 'Text'" /tmp/test.png

what is x, y means in text x, y 'string'?

回答1:

Method 1 label:

You can either use label: which provides a canvas large enough to hold your text - notice I didn't provide a canvas size:

convert -background yellow label:"This is some\nmulti-line text\nwhich\nsurprisingly enough -\nspans multiple lines" image.png

Method 2 -annotate

Or, you can use -annotate to write onto an outsize canvas, then -trim the canvas back to the minimum afterwards:

convert -size 1000x1000 xc:blue -pointsize 30 -gravity northwest -annotate 0 'Text\nwith multiple\nlines.' -trim result.png

When using -annotate, be sure to specify -gravity so that it positions according to the bounding box of your text, because if you do not, it will position according to the baseline (bottom-left corner) of your text.

Method 3 caption:

Or, you can use caption: which will size the text to best fit the box you provide:

convert -background pink -fill white -size 400x100 caption:"Here is a bunch of text that will be sized to best fit the box as far as is possible" result.png



回答2:

In the ImageMagick "-draw text x,y string" option, "x,y" is the position of the bottom* left of the text measured from the the top left of the image. You can use the "-gravity" option to measure from other corners of the image, for example,

convert -size 1000x100 xc:transparent -pointsize 30 -draw "gravity southwest text 0,30 'Text'" ...

will place the lower left corner* of the text 30 pixels above the lower left corner of the image.

*strictly speaking, it's the left end of the baseline of the text; if there are symbols with descenders such as "g" or "y", those extend below the baseline. Demonstrate that with "-text 0,0 goody" and you'll just see the descenders extending into the top of the image.

magick -size 500x100 xc:yellow -pointsize 90 \
       -draw "text 0,0 'goody 0,1'" \
       -draw "text 0,100 'goody 0,100'" text.png

produces