Vertical alignment of 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
Santa911
Posts: 11
Joined: 2011-04-20T23:24:46-07:00
Authentication code: 8675308

Vertical alignment of text

Post by Santa911 »

I wish to use IM to write text (month names) on some pictures using commandlines. I would like the text to be rotated 270 degrees and placed to the left on the picture. My problem is that I can't figure out how to center the text vertically. The problem is that the lenght of the text is not the same, eg. May and September so I can't use offset.

I have no problem using the code "-gravity West" when the text is not rotated, but the text is not centered when rotated 270 degrees eg. "-annotate 270 February".

Can somebody help me with relevant code?

Lars
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Vertical alignment of text

Post by Bonzo »

I would generate the text as an image and then put it on the photo. There can be a problem using -gravity on the text then locating an image using -gravity so may need to be done in two commands.

Untested and avoids using -gravity in the text generation:

Code: Select all

convert photo.jpg -gravity west ( -size 50x100 xc:none -fill black -annotate 90 "April" -trim ) -composite +repage output.jpg
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Vertical alignment of text

Post by anthony »

As you are rotating all the text 270 degrees, why not just generate the text normally (center aligned or otherwise) then rotate the resulting image before overlaying on the background image.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Santa911
Posts: 11
Joined: 2011-04-20T23:24:46-07:00
Authentication code: 8675308

Re: Vertical alignment of text

Post by Santa911 »

Thank you for your constructive input. I have tried to work with it, but I run into problems. The suggested commandline:
convert photo.jpg -gravity west ( -size 400x1200 xc:green -fill black -pointsize 120 -annotate 90 "September" -trim ) -composite +repage output.jpg
gives the result shown here:
http://www.dummyweb.dk/Temp/outputEx0.jpg

I have problems moving the text/image a bit further to the right. I would like to have this result (without the red color...):
http://www.dummyweb.dk/Temp/outputEx4.jpg

This however I can only get to work with this command which is not general:
convert photo.jpg -gravity west ( -size 1200x1200 xc:red -fill black -pointsize 150 -geometry -900+0 -gravity center -annotate 270 "September" -trim ) -composite +repage output.jpg

If the picture has a different size then the -geometry -900+0 is a bad solution. However I can't get this to work:
convert photo.jpg -gravity west -geometry +200+0 ( -size 1200x1200 xc:red -fill black -pointsize 150 -gravity center -annotate 270 "September" -trim )
-composite +repage output.jpg
It gives me this result:
http://www.dummyweb.dk/Temp/outputEx5.jpg

Hope somebody can help me with moving the text/picture a bit from the "west gravity"

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

Re: Vertical alignment of text

Post by fmw42 »

Your pointsize is too big for the size you specified. Better to let label: determine the best pointsize by only specifying one size and leaving off pointsize. Also you need to specify -gravity and -geometry after the overlay image in a convert ... -composite.

Try this and adjust the -size and -geometry to suit.


convert input ( -size 600x -background red -fill black label:"September" -rotate 90 -trim +repage ) -gravity west -geometry +20+0 -composite output


Also you might like to try this as an alternate to get the text vertically stacked using line feeds between characters.

convert input ( -size x600 -background red -fill black -gravity center label:"S\ne\np\nt\ne\nm\nb\ne\nr" -trim +repage ) -gravity west -geometry +20+0 -composite output
Santa911
Posts: 11
Joined: 2011-04-20T23:24:46-07:00
Authentication code: 8675308

Re: Vertical alignment of text

Post by Santa911 »

I got it to work now with all your help.

My result for a photocalender now looks like this:

http://www.dummyweb.dk/Temp/DummyKalender2011-02.jpg

Thank you for all your help - it was very useful :-)

Lars
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Vertical alignment of text

Post by Bonzo »

Thanks for the link Lars as its good to see the final result.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Vertical alignment of text

Post by anthony »

There is a small sed-tr script in the example shown in
IM Examples, Text Handling, Vertical Labels
http://www.imagemagick.org/Usage/text/#label_vertical
which will insert a newline character between every letter. Also note the use of gravity center too.

However I see from your 'final result' that you just decided to rotate the month name, instead of using a vertical stack
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply