Page 1 of 1
Locating precise text position
Posted: 2017-06-28T06:22:44-07:00
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
Re: Locating precise text position
Posted: 2017-06-28T07:17:59-07:00
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.
Re: Locating precise text position
Posted: 2017-06-28T09:44:02-07:00
by GeeMack
djd wrote: ↑2017-06-28T06:22:44-07:00Given 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.
Re: Locating precise text position
Posted: 2017-06-28T17:50:18-07:00
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'.
Re: Locating precise text position
Posted: 2017-06-28T21:34:05-07:00
by GeeMack
djd wrote: ↑2017-06-28T17:50:18-07:00I 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...
Find a lengthy description
at THIS link.
Re: Locating precise text position
Posted: 2017-06-29T09:32:50-07:00
by djd
Thanks for the info GeeMack