How to center text?

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
agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

How to center text?

Post by agriz »

How to center text according to this image on those particular boxes?
Image

Thanks
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to center text?

Post by anthony »

How were the boxes made? Just in a previously given image, or drawn?
Are they static or can they move about? Do they vary in size?

What do you want to know for? -- their may be alternative solutions.

Basically what do we have to work with?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

Re: How to center text?

Post by agriz »

Assuming that the boxes are static in size, and which is already fixed with the original image, how can we center the text?

[if we can't create similar box and composite on the original image, how can we center the text?]
agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

Re: How to center text?

Post by agriz »

I can use -gravity and -annotate but what can i do if the text size is not same all the time?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to center text?

Post by fmw42 »

always use offset coordinates relative to the center of the image to the center of the boxes using -gravity center
agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

Re: How to center text?

Post by agriz »

can you just give any example? i didn't know how to give offset coordinates ...
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to center text?

Post by Bonzo »

-gravity center will find the center of the image and you can offset it with -geometry +10+10
If you are using -annotate you can offset it in the -annotate command -annotate +10+10 "text"
agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

Re: How to center text?

Post by agriz »

Yes, But how can i center dynamic text with different number of characters?

ex : "This is funny", "booo", "hon", ...
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to center text?

Post by Bonzo »

If it was me I would write the text into a box with a transparent backgound ( caption or label ) then find the size of the box bounding the text and do some calculations on placing the text in the correct place.

You do not say how you are running the code but you may have to use fx for the calculations if you have no other way of doing the calculations.
agriz
Posts: 237
Joined: 2011-10-01T02:21:30-07:00
Authentication code: 8675308

Re: How to center text?

Post by agriz »

Yes, It is good idea. I will try the transparent background method and i will let you know.
It will work perfectly, I guess :)
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: How to center text?

Post by Bonzo »

Using caption or label the bounding box size will change that is why I said you may need to use fx etc. to calculate the position.

If you always use a box the same size and center the text in the box the position will be the same every time when compositing.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to center text?

Post by fmw42 »

Yes, But how can i center dynamic text with different number of characters?

ex : "This is funny", "booo", "hon", ...
If you write your text using -annotate with -gravity center, it will center the text at the offset location. At least that is my understanding. Thus it does not matter how long the text is, so long as it does not exceed your box size.

The same for label: and caption: if you create those images in center justified mode and then use -gravity center and -geometry to place them at the center of your box. The only computations from fx: would be to compute the box coordinates center relative and not top left relative.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: How to center text?

Post by anthony »

agriz wrote:I can use -gravity and -annotate but what can i do if the text size is not same all the time?
I would generate a gravity centered 'caption:' of the appropriate size (with or without a pointsize), and then layer that caption on the background image.
http://www.imagemagick.org/Usage/text/#caption
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply