0

I successfully draw some text on an image by pgmagick:

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

but how to get the bounding box of text? Anyone can help me?

1 Answers1

0

I don't know anything about pgmagick but maybe it will help if I show you a couple of things you can do at the command line with ImageMagick.

Option 1

Use label to create a canvas just big enough for the desired text and then ask ImageMagick how big that canvas's geometry is:

convert label:"Hello world" -format %G info:
62x15

So, "Hello world" in the default font size is 62 pixels wide and 15 pixels high.

Option 2

Or, use -annotate as you did, and then ask ImageMagick how big that would be if you trimmed the excess space around it:

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

Option 3

Create the canvas and annotate it, then trim it and get the size:

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

Option 4

Create canvas, annotate, trim, save and then get dimensions of result:

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

Maybe (hopefully) you can adapt one of these to pgmagick.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432