Single letter labels are not centered properly

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
tabicat
Posts: 2
Joined: 2011-10-28T19:09:22-07:00
Authentication code: 8675308

Single letter labels are not centered properly

Post by tabicat »

Hi

This command displays a message centered inside a black rectangle.

Code: Select all

convert -size 30x30 -background '#00000080' -fill white -gravity center label:ABC test1.png
Image

However, if I tell it to write a single character, the alignment is off:

Code: Select all

convert -size 30x30 -background '#00000080' -fill white -gravity center label:A test2.png
Image

In this case, the letter "A" touches the top of the box. How can I fix this?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Single letter labels are not centered properly

Post by anthony »

The size of the label is designed to allow you to append labels together. That includes labels of lower case letters that have descenders!!! like 'y'

Also note that you do not have 'pointsize' set so the font is sized to 'best fit' the box, as best it can. It is also very font specific.

Best idea is the trim and enlarge the label to 'full center it.

Code: Select all

convert -size 30x30 -background 'none' -fill white -gravity center label:A \
       -trim +repage -gravity center -background '#00000080' -extent 30x30 test3.png
Note I only set background once as you are using a semi-transparent background!
If I didn't the 'overlay' performed by extent will result in a semi-transparency over a seni-transparency whcih does not look right. Using opaque backgrounds, or full-transparency (above) do not have these problems.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply