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
Locating precise text position
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Locating precise text position
"-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
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Locating precise text position
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:
Re: Locating precise text position
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'.
Removing sed and leaving everything to IM appeals as the more `elegant' way.
I also need to become familiar with `fx'.
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Locating precise text position
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:
Code: Select all
... "%[fx:w-$F]" ...
Re: Locating precise text position
Thanks for the info GeeMack