获取图像上文本的几何形状

Get geometry of text on an image

我想用这样的方式在图像上绘制文本:

convert -quality 100 -font Oswald-Regular -pointsize 515 -fill black -draw "text 1339.0,1099 'some text'" /tmp/ascript.png /tmp/ascript.png

并且我需要知道具有上述参数(大小、字体、文本)的文本的尺寸。我怎样才能得到它?

我试过这样的事情:

convert -size 5000x1500 xc:lightblue -font Oswald-Regular -pointsize 515 -fill none -undercolor white -annotate +20+100 'some text' -trim  info:

但它给出了错误的结果:

xc:lightblue XC 1834x250 5000x1500+19+0 16-bit sRGB 0.010u 0:00.000

.

根据这 3 个参数(字体、大小、文本)获取绘制图像尺寸的正确方法(或工作方法)是什么?

我并没有严格绑定ImageMagick,它可以是Linux shell的任何命令行工具,但是,文本将通过convert绘制。

有几种简单的方法可以通过像这样的命令使用 ImageMagick 获取尺寸...

convert -size 5000x1500 xc:lightblue -font Oswald-Regular -pointsize 515 \
    -fill none -undercolor white -annotate +20+100 'some text' \
    -format "%[@]\n" info:

使用 FX 转义符“%@”作为“info:”输出的格式字符串。它将显示 IM 计算后-trim 宽度、高度、水平偏移和垂直偏移,如“WxH+X+Y”。

这个类似的命令只是给出 trimmed 文本的宽度和高度...

convert -size 5000x1500 xc:lightblue -font Oswald-Regular -pointsize 515 \
    -fill none -undercolor white -annotate +20+100 'some text' \
    -trim +repage -format "%[w]x%[h]\n" info:

这将 trim 文本,使用“+repage”重置分页几何,然后输出显示“WxH”的字符串。

––– 编辑添加–––

我用这些命令试过你的图像 with_text.png。输出紧跟在每个命令之后...

convert with_text.png -format "%[@]\n" info:
1807x389+512+115

convert with_text.png -trim +repage -format "%[w]x%[h]\n" info:
1807x389

这些已在 ubuntu bash 和 Windows 10 上使用 IMv6.8.9-9 进行了测试。如果你使用那个实际图像和那些命令,我​​不确定你为什么会得到不同的结果。