-draw centering text from absolute XY coords

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?".
Post Reply
SamL
Posts: 3
Joined: 2011-11-11T09:12:15-07:00
Authentication code: 8675308

-draw centering text from absolute XY coords

Post by SamL »

Dear Magicians..

I am pulling a set of xy coords from a database. These xy coords are supposed to be the center point from where text would become centered. I've tried every combination of gravity to accomplish this to no avail. Keep in mind, that the only coords that I have, are the CENTER POINT from where the text should be centered. Using the command below, I can get the text overlayed onto the image perfectly, beginning at the XY coords, but not centered from there:

Imagemagick command: /usr/bin/convert "/var/dgfiles/automate/incoming/aip4-test.jpg" -font "/var/dgfiles/automate/fonts/TitilliumText22L003.otf" -pointsize 38 -fill white -draw "text 435,524 \"Apocalypse Blue\"'" -font "/var/dgfiles/automate/fonts/TitilliumText22L003.otf" -pointsize 27 -fill white -draw "text 435,560 'by Girsl Collective'" -font "/var/dgfiles/automate/fonts/Museo_Slab_500.otf" -pointsize 58 -fill white -draw "text 435,1023 'F66981DFAIP4'" "/var/dgfiles/automate/outgoing/aoc-test.jpg"

ANY TIPS would be hugely appreciated.

Thank you,
Sam
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -draw centering text from absolute XY coords

Post by fmw42 »

Use -annotate. Something like this seems to work for me on IM 6.7.3.5 Q16 Mac OSX Tiger


x=150
y=150
w=200
h=200
ox=`convert xc: -format "%[fx:$x-$w/2]" info:`
oy=`convert xc: -format "%[fx:$y-$h/2]" info:`
convert -size ${w}x${h} xc:white -gravity center -font Arial -pointsize 12 \
-annotate +$ox+$oy "TESTING" tmp.png


The issue will be that you will need to compute the sign for ox and oy as IM does not work when you do +-10+-10, nor will it work if you do $ox$oy because it needs a sign and you wont get a + sign from the variables. So compute the sign and absolute value for ox and oy and create your offsets to -annotate appropriately with the proper sign such as +10-10.

If you need further help on this, let me know as I have done that before.
SamL
Posts: 3
Joined: 2011-11-11T09:12:15-07:00
Authentication code: 8675308

Re: -draw centering text from absolute XY coords

Post by SamL »

Are you saying the only way this can work is if i know the size of the area the text is supposed to be placed within??
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: -draw centering text from absolute XY coords

Post by fmw42 »

SamL wrote:Are you saying the only way this can work is if i know the size of the area the text is supposed to be placed within??
I think so. But I will defer to the IM developers. Perhaps there is some way that I do not know about.
SamL
Posts: 3
Joined: 2011-11-11T09:12:15-07:00
Authentication code: 8675308

Re: -draw centering text from absolute XY coords

Post by SamL »

I think I might be getting somewhere but am not really sure...

This command produces text at the center of the image as expected:
/usr/bin/convert "/var/dgfiles/automate/incoming/aip4-test.jpg" -font "/var/dgfiles/automate/fonts/TitilliumText22L003.otf" -pointsize 38 -fill White -gravity Center -draw "text 0,0 'Apocalypse Blue'" "/var/dgfiles/automate/outgoing/aoc-test.jpg"

However, when I try to apply a region to the command, I no longer see the text...
/usr/bin/convert "/var/dgfiles/automate/incoming/aip4-test.jpg" -font "/var/dgfiles/automate/fonts/TitilliumText22L003.otf" -pointsize 38 -fill White -gravity Center -region 489x42+190+502 -draw "text 0,0 'Apocalypse Blue'" "/var/dgfiles/automate/outgoing/aoc-test.jpg"

My thought was that I could create a 'region' then somehow place the text in the center of that region?

Is that possible?
Thank you,
Sam
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: -draw centering text from absolute XY coords

Post by Bonzo »

Another idea may be to create a label and put the label on the image:

Code: Select all

convert -background none -fill blue -pointsize 70 label:Anthony miff:- | composite - london.png -gravity center -geometry +100+100 output.jpg

convert london.png ( -background none -fill blue -pointsize 70 label:Anthony ) -gravity center -geometry +100+100 -composite output.jpg
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: -draw centering text from absolute XY coords

Post by anthony »

SamL wrote:Are you saying the only way this can work is if i know the size of the area the text is supposed to be placed within??
At this time yes. The separation of text (and image) justification, from the gravity positioning setting is something that will be looked at early is IMv7 development. We are currently only in alpha at this time.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply