image-processing - 获取图像上文本的几何形状
问题描述
我想以这样的方式在图像上绘制文本:
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 对修剪后宽度、高度、水平偏移和垂直偏移的计算,如“WxH+X+Y”。
这个类似的命令只是给出了修剪文本的宽度和高度......
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:
这将修剪文本,用“+repage”重置分页几何,然后输出一个显示“WxH”的字符串。
––– 编辑添加–––</p>
我用这些命令尝试了你的图片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
这些在 Windows 10 上的 ubuntu bash 上使用 IMv6.8.9-9 进行了测试。如果您使用该实际图像和那些命令,我不确定为什么会得到不同的结果。