Search code examples
bashimagemagickimage-manipulationimagemagick-convert

Center text vertically but not horizontally in specified box imagemagick


I'm trying to generate image from text. Requirements are: text left aligned, vertically centered, big as possible, max. resolution 1920x1080.

This is what I have:

convert -background white -fill black \
-font "fonts/DejaVuSansMono.ttf" \
-size 1920x1080 label:'Text \nloooooooooooooooooooooooooong text\nand another' \
-gravity West image.png

It works pretty well, but it doesn't center it to vertically.

Looks like this: https://i.sstatic.net/dGHZg.png

But I would like it to look like this: https://i.sstatic.net/0JxKk.png

I could use -size x1080 and put that image into center of a blank white image using second command, but that doesn't make sure it is not too wide.


Solution

  • Try setting the gravity before creating the label....

    convert -background white -fill black -size 1920x1080 \
       -gravity west \
       label:'Text \nloooooooooooooooooooooooooong text\nand another' \
       image.png
    

    Edited to add: When a setting is supposed to affect a particular operation, in almost every case with ImageMagick the setting should come ahead of the operation. IM version 6 is somewhat forgiving in that regard, but as with your example, often the order of the command is important. IM version 7 is much more strict.