Centered text in imported image

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
rsperes
Posts: 5
Joined: 2014-11-24T05:04:01-07:00
Authentication code: 6789

Centered text in imported image

Post 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)…

Image
Last edited by rsperes on 2014-11-24T05:43:08-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Centered text in imported image

Post by snibgo »

convert example.jpg -pointsize 200 -gravity Center -annotate 0 "Hello world" y.png
snibgo's IM pages: im.snibgo.com
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Centered text in imported image

Post by Bonzo »

Is the "marked area" bordered by the red box?
rsperes
Posts: 5
Joined: 2014-11-24T05:04:01-07:00
Authentication code: 6789

Re: Centered text in imported image

Post by rsperes »

yes Bonzo…the red area...
rsperes
Posts: 5
Joined: 2014-11-24T05:04:01-07:00
Authentication code: 6789

Re: Centered text in imported image

Post 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!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Centered text in imported image

Post 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
snibgo's IM pages: im.snibgo.com
rsperes
Posts: 5
Joined: 2014-11-24T05:04:01-07:00
Authentication code: 6789

Re: Centered text in imported image

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Centered text in imported image

Post 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.
snibgo's IM pages: im.snibgo.com
rsperes
Posts: 5
Joined: 2014-11-24T05:04:01-07:00
Authentication code: 6789

Re: Centered text in imported image

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Centered text in imported image

Post 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?
snibgo's IM pages: im.snibgo.com
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Centered text in imported image

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Centered text in imported image

Post 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
Post Reply