Locating precise text position

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Locked
djd
Posts: 41
Joined: 2011-11-12T22:20:18-07:00
Authentication code: 8675308

Locating precise text position

Post by djd »

The following seems to work (at least with two test numerics) but it appears overly complex. Is there a better way?

# Given text of arbitary font size inside a box, find the x-position of
# the first pixel of the text

F=27

imgconvert -size 40x50 xc:none -pointsize 13 -fill black -gravity center \
-annotate 0 "$F" -trim info: | sed 's/[^+]*+\([0-9]*\)+.*/\1/'
12

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Locating precise text position

Post by snibgo »

"-trim" calculates where the new boundary should be, then creates a new image in memory. This second step isn't needed, so you could replace "-trim" with "-format %@". But then you would need to change the sed command to suit.
snibgo's IM pages: im.snibgo.com

User avatar
GeeMack
Posts: 757
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Locating precise text position

Post by GeeMack »

djd wrote:
2017-06-28T06:22:44-07:00
Given text of arbitary font size inside a box, find the x-position of the first pixel of the text
You can access the x offset directly. Using IM 6.7.7.-10 from a bash shell I can do this...

Code: Select all

F=27

convert -size 40x50 xc:none -pointsize 13 -fill black \
   -gravity center -annotate 0 "$F" -trim -format %[fx:page.x] info:
The output will be the number of transparent pixels that were trimmed from the left. You can leave the "sed" command out of it entirely.

djd
Posts: 41
Joined: 2011-11-12T22:20:18-07:00
Authentication code: 8675308

Re: Locating precise text position

Post by djd »

Thanks to both snibgo and GeeMack.
Removing sed and leaving everything to IM appeals as the more `elegant' way.
I also need to become familiar with `fx'.

User avatar
GeeMack
Posts: 757
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Locating precise text position

Post by GeeMack »

djd wrote:
2017-06-28T17:50:18-07:00
I also need to become familiar with `fx'.
The FX expression language gives you access to tons of information and lets you do nearly any sort of calculation with the data. In your example, after the "-trim" operation (and before a "+repage"), the working copy still knows its original width and height, the left and top trim locations, its current width and height, and more.

You could, for example, find the amount trimmed off the bottom with something like this...

Code: Select all

F=27

convert -size 40x50 xc:none -pointsize 13 -fill black \
   -gravity center -annotate 0 "$F" -trim -format "%[fx:page.height-(h+page.y)]" info:
Or even subtract your variable "$F" from the width like this...

Code: Select all

... "%[fx:w-$F]" ...
Find a lengthy description at THIS link.

djd
Posts: 41
Joined: 2011-11-12T22:20:18-07:00
Authentication code: 8675308

Re: Locating precise text position

Post by djd »

Thanks for the info GeeMack

Locked