Page 1 of 1
Centered text in imported image
Posted: 2014-11-24T05:09:20-07:00
by rsperes
Hi, how can I write a centered text in a portion of a imported image? I've tried everything but couldn't position the text using gravity center. The text is dynamic…
This is the example image. The text the user types in must appear centred in the marked area (red square)…

Re: Centered text in imported image
Posted: 2014-11-24T05:28:53-07:00
by snibgo
convert example.jpg -pointsize 200 -gravity Center -annotate 0 "Hello world" y.png
Re: Centered text in imported image
Posted: 2014-11-24T05:42:06-07:00
by Bonzo
Is the "marked area" bordered by the red box?
Re: Centered text in imported image
Posted: 2014-11-24T05:43:29-07:00
by rsperes
yes Bonzo…the red area...
Re: Centered text in imported image
Posted: 2014-11-24T05:47:22-07:00
by rsperes
Bonzo wrote:Is the "marked area" bordered by the red box?
yes Bonzo…it's the red area. It's driving me crazy!
Re: Centered text in imported image
Posted: 2014-11-24T05:57:18-07:00
by snibgo
Find the offset of the centre of the red box with respect to the centre of the image. This is roughly -300, +150
Code: Select all
convert example.jpg -pointsize 100 -gravity Center -annotate -300+150 "Hello world" y.png
Re: Centered text in imported image
Posted: 2014-11-24T06:06:26-07:00
by rsperes
snibgo wrote:Find the offset of the centre of the red box with respect to the centre of the image. This is roughly -300, +150
Code: Select all
convert example.jpg -pointsize 100 -gravity Center -annotate -300+150 "Hello world" y.png
Hi snibgo. It worked. How do you get -300+150 values?
Re: Centered text in imported image
Posted: 2014-11-24T06:20:42-07:00
by snibgo
With Gimp, I saw that the centre of the red box was roughly 300 pixels left of the centre of the image, and 150 pixels lower. You should use accurate numbers.
Re: Centered text in imported image
Posted: 2014-11-24T11:46:53-07:00
by rsperes
snibgo wrote:With Gimp, I saw that the centre of the red box was roughly 300 pixels left of the centre of the image, and 150 pixels lower. You should use accurate numbers.
My happiness does not last long…The alignment gets messy if the text is to short…There's a more precise way to deal with it that adjust to the length of text?
Re: Centered text in imported image
Posted: 2014-11-24T11:53:37-07:00
by snibgo
rsperes wrote:The alignment gets messy if the text is to short…There's a more precise way to deal with it that adjust to the length of text?
I don't understand.
What happens when the text is too short?
Re: Centered text in imported image
Posted: 2014-11-24T14:31:10-07:00
by Bonzo
As snibgo says you need to give more information about the problem.
An alternative method would be to write the text to a canvas and then composite that canvas over the image:
Code: Select all
convert example.jpg ( -size 540x230 xc:none -gravity center -pointsize 50 -annotate 0 "Hello world" ) -gravity northwest -geometry +120+720 -composite output.jpg
Re: Centered text in imported image
Posted: 2014-11-24T15:56:01-07:00
by fmw42
I would suggest you use caption: so that long text is wrapped. Also select a region that provides a bit or margin. I opened your file in another tool, such as GIMP or Photoshop and picked a rectangular region inside your red box. That provided the -size argument and the -geometry argument.
Unix syntax:
Code: Select all
convert example.jpg \
\( -size 508x187 -background none -gravity center -pointsize 50 -fill black caption:"Hello" \) \
-gravity northwest -geometry +135+742 -composite output.jpg
Code: Select all
convert example.jpg \
\( -size 508x187 -background none -gravity center -pointsize 50 -fill black \
caption:"Now is the time for all good men to come to the aid of their country" \) \
-gravity northwest -geometry +135+742 -composite output2.jpg