我通过 pgmagick 在图像上成功绘制了一些文本:
from pgmagick.api import Image
img = Image((300, 200))
img.annotate('Hello World')
img.write('helloworld.png')
但是如何获得文本的边界框?任何人都可以帮助我吗?
我通过 pgmagick 在图像上成功绘制了一些文本:
from pgmagick.api import Image
img = Image((300, 200))
img.annotate('Hello World')
img.write('helloworld.png')
但是如何获得文本的边界框?任何人都可以帮助我吗?
我对此一无所知,pgmagick
但如果我向您展示一些您可以在命令行中使用ImageMagick执行的操作,这可能会有所帮助。
选项1
用于label
为所需文本创建一个足够大的画布,然后询问ImageMagick该画布的几何图形有多大:
convert label:"Hello world" -format %G info:
62x15
因此,“Hello world”的默认字体大小为 62 像素宽和 15 像素高。
选项 2
或者,-annotate
像你一样使用,然后询问ImageMagick,如果你修剪它周围的多余空间,它会有多大:
convert -size 300x200 xc:red -annotate +10+20 "Hello world" -format %@ info:
61x8+10+12
选项 3
创建画布并对其进行注释,然后对其进行修剪并获取大小:
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
选项 4
创建画布、注释、修剪、保存,然后获取结果的尺寸:
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
也许(希望)您可以将其中之一改编为pgmagick
.