如何使用pgmagick绘制文本边界框

时间:2016-10-21 09:47:54

标签: imagemagick graphicsmagick pgmagick

我通过pgmagick在图像上成功绘制了一些文字:

from pgmagick.api import Image
img = Image((300, 200))
img.annotate('Hello World')
img.write('helloworld.png')

但如何获取文本的边界框?有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

我对[{1}}一无所知,但如果我通过 ImageMagick 在命令行向您展示一些事情,它可能会有所帮助。

选项1

使用pgmagick创建一个足以容纳所需文本的画布,然后询问 ImageMagick 该画布的几何体有多大:

label

所以," Hello world"默认字体大小为62像素宽,15像素高。

选项2

或者,像你一样使用convert label:"Hello world" -format %G info: 62x15 ,然后问 ImageMagick 如果你修剪它周围的多余空间会有多大:

-annotate

选项3

创建画布并对其进行注释,然后修剪它并获得大小:

convert -size 300x200 xc:red -annotate +10+20 "Hello world" -format %@ info:
61x8+10+12

选项4

创建画布,注释,修剪,保存,然后获取结果的维度:

convert -size 300x200 xc:red -annotate +10+20 "Hello world" -trim info:
xc:red XC 61x8 300x200+10+12 16-bit sRGB 0.000u 0:00.000

也许(希望)您可以将其中一个调整为convert -size 300x200 xc:red -annotate +10+20 "Hello world" -trim result.png identify result.png result.png PNG 61x8 300x200+10+12 16-bit sRGB 887B 0.000u 0:00.000