ImageMagick最适合在矩形内的文字? [英] ImageMagick best fit text within rectangle?
问题描述
我有一个这样的图像,在特定坐标处有一个矩形:
(为了说明的目的,我把矩形的坐标及其大小和中心放在那里)
现在我想用ImageMagick渲染一些文本,这样它完全符合矩形。
如果它是一个非常短(窄)的字符串,矩形的高度将是限制因素:
另一方面,长(宽)的字符串,矩形的宽度将决定大小:
无论是哪种情况,独立于文本的短或短,我想打印一行(即没有文字换行或多行),并且恰好适合直肠ngle,并确保它以为中心(文本的中心位于矩形的中心)。
我的任务:
-
如何用ImageMagick做这个最合适功能(我不知道如何动态地确定所需的
-pointsize
为此) -
如何使文本居中,当我使用
重力中心
它似乎适用于整个图像中文本的位置,即文本坐标变成相对于整个图像的中心。但是我想要指定精确(绝对)坐标,而这应该是文本的中心。
例如,如果我这样做:
convert test.jpg -font Arial -fill yellow \
-pointsize 65 - 绘制文本398,90'你好'test2.jpg
我得到:
<注意我指定的坐标(矩形的中心)将成为文本的
如果我这样做:
convert test.jpg -font Arial -fill yellow \
$我得到:
-pointsize 65 -gravity center -drawtext 148,-94'Hello'test3.jpg
哪个可以,但请注意我必须使用的奇怪的文本坐标。除此之外,我还不知道如何自动计算pointize(通过反复验错)。
解决方案更新答案
根据我现在收集的内容,我认为这可能是您最好的选择。
convert sea.jpg \(-size 173x50 -background none label:A-trim -gravity center -extent 173x50 \)-gravity northwest-geometry + 312 + 66 -composite result.png
这样:
convert sea.jpg \(-size 173x50 -background无标签:一个非常长的标签-trim -gravity center -extent 173x50 \)-gravity northwest-geometry + 312 + 66 -composite result.png
基本上,我在括号中使用一些处理来生成文本,然后将其合成到页面上。我将文本生成
label:
到最佳自动大小,然后剪裁文本周围的多余空间。然后,使用-gravity center
并将背景展开(使用-extent
),使中心修剪文本,以便文本框总是相同的大小,然后当我重新设置时,我可以将其定位(使用
到-geometry
)合成相对于左上角 - 重力NorthWest
。
原始答案 / p>
如果您希望ImageMagick尽力在一个给定的框中适合您的文本,您应该使用
caption
而不是注释
,标签
或-drawtext
/ p>
所以你想加载海图,设置标题的大小,使用
在正确的位置绘制标题并将其复合到图像上, -geometry
:convert sea.jpg -size 173x50!标题:您的文字-geometry + 312 + 66 -composite result.png
或者,文字较长:
convert sea.jpg -size 173x50!标题:相当长的文本将导致选择较小的字体-geometry + 312 + 66 -composite result.png
如果你想要一个空白背景,在
之前使用
-background none
标题:convert sea.jpg -size 173x50! -background none caption:Your text-geometry + 312 + 66 -composite result.png
< a href =https://i.stack.imgur.com/YOWIj.png =nofollow noreferrer>
如果您想让文字居中,可以使用PANGO,像这样,但我相信你然后放弃自动调整功能:
convert sea.jpg -size 173x50! -background none -gravity center pango:'< span foreground =yellow> ABC< / span>'-gravity northwest-geometry + 312 + 66 -composite result.png
I have an image like this, with a rectangle at specific coordinates:
(for illustratory purposes I put the coordinates of the rectangle and its size and center in there)
Now I want to render some text with ImageMagick, so that it fits exactly within the rectangle.
If it's a very short (narrow) string, the rectangle's height will be the limiting factor:
On the other hand with a long (wide) string, the rectangle's width will determine the size:
In either case, independent of how short or long the text is, I would like to print it in one line (i.e. no word wrapping or multi line), and have it fit exactly in the rectangle, and make sure it's centered (the center of the text is in the center of the rectangle).
My questons:
How to do this 'best fit' feature with ImageMagick (I don't know how to dynamically determine the required
-pointsize
for this)How to get the text centered, when I use
-gravity center
it seems to apply to the position of the text within the entire image, i.e. text coordinates become relative to the entire image's center. But I want to specify exact (absolute) coordinates, and that should be the center of the text.For example, if I do this:
convert test.jpg -font Arial -fill yellow \ -pointsize 65 -draw "text 398,90 'Hello'" test2.jpg
I'm getting:
Note how the coordinates I specify (the rectangle's center) become the bottom left anchor point for the text! (this surprised me)
And if I do:
convert test.jpg -font Arial -fill yellow \ -pointsize 65 -gravity center -draw "text 148,-94 'Hello'" test3.jpg
I get:
Which is kinda OK, but note the weird text coordinates I have to use to get that. And besides I wouldn't know how to automatically calculate the pointsize (did the above by trial and error).
解决方案Updated Answer
With what I have now gathered, I think this may be your best option.
convert sea.jpg \( -size 173x50 -background none label:"A" -trim -gravity center -extent 173x50 \) -gravity northwest -geometry +312+66 -composite result.png
And this:
convert sea.jpg \( -size 173x50 -background none label:"A very much longer label" -trim -gravity center -extent 173x50 \) -gravity northwest -geometry +312+66 -composite result.png
Basically, I am using some "aside processing" in parentheses to generate the text and then compositing it onto the page afterwards. I generate the text with
label:
to the best automatic size, then trim off any excess space around the text. I then centre the trimmed text, using-gravity center
and expand the background out (using-extent
) so that the text box is always the same size, then I can position it (with-geometry
) when compositing it relative to the top-left corner as I reset-gravity
toNorthWest
.Original Answer
If you want ImageMagick to do its best to fit your text in a given box, you should use
caption
rather thanannotate
,label
or-draw "text"
.So you want to load your sea image, set the size for the caption, draw the caption and composite that onto the image at the correct spot using
-geometry
:convert sea.jpg -size 173x50! caption:"Your text" -geometry +312+66 -composite result.png
Or, with longer text:
convert sea.jpg -size 173x50! caption:"A considerably longer text that will result in a smaller font being chosen" -geometry +312+66 -composite result.png
If you want a blank background, use
-background none
beforecaption:
convert sea.jpg -size 173x50! -background none caption:"Your text" -geometry +312+66 -composite result.png
If you want to centre your text, you can do that with PANGO, like this, but I believe you then give up the auto-sizing feature:
convert sea.jpg -size 173x50! -background none -gravity center pango:'<span foreground="yellow">ABC</span>' -gravity northwest -geometry +312+66 -composite result.png
这篇关于ImageMagick最适合在矩形内的文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!