Center a text problem

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?".
Sardoan
Posts: 10
Joined: 2017-10-17T10:57:05-07:00
Authentication code: 1151

Center a text problem

Post by Sardoan » 2017-10-17T23:33:39-07:00

Hi,
I am trying to generate an avatar with the initials of the user. Sounds simple, there are some examples like

Code: Select all

convert -background lightblue -fill blue \
          -font Candice -pointsize 72 label:Anthony \
          label.gif
Somehow gravity center is not working correctly with all characters. If I use "MR" as label with Open Sans or Roboto as font, ImageMagick cannot calculate the correct width. After a -trim the "R" is getting cut off on the right side. So "MR" is not beeing rendered at the center. Does anyone have a woraround for it? When I add underscore to the label "_MR_" gravity center works fine. So it seems it works with some characters, but not with all.

I thought about adding the underscores with the backgroundcolor, but since I want to use a gradient for the background it will not work.

snibgo
Posts: 9382
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Center a text problem

Post by snibgo » 2017-10-18T00:14:52-07:00

I don't understand how a "-trim" is cutting off anything. Perhaps you can show example images.

Adding a space before and after the text might help, eg:

Code: Select all

label: " MR "
The general solution may be to not use IM's feature of automatically calculating the appropriate image size. Instead, provide a generous "-size" for the image, write the label, trim the image, then finally add a border, if you want.
snibgo's IM pages: im.snibgo.com

Sardoan
Posts: 10
Joined: 2017-10-17T10:57:05-07:00
Authentication code: 1151

Re: Center a text problem

Post by Sardoan » 2017-10-18T00:56:09-07:00

I had an avatar with border, trim was cutting the "R" off.
Here is an example without trim and without the border:
Image

As you can see gravity center is not working in this case.
Right side of the "R" was cut off.

Sardoan
Posts: 10
Joined: 2017-10-17T10:57:05-07:00
Authentication code: 1151

Re: Center a text problem

Post by Sardoan » 2017-10-18T01:09:43-07:00

Removed gravity and it worked:
Image

But only for this case. Tried it with "WW" and this came out:
Image

How can I achiev a vertical align middle?

And by the way, working with " MR " is not working, becase ImageMagick only recognizes the right space.

Image

With gravity: Left with " WW " and right "_WW_".

And here the picture with -trim, which cuts off the lower right of the "R":
Image

snibgo
Posts: 9382
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Center a text problem

Post by snibgo » 2017-10-18T01:46:00-07:00

It would help if you showed your code. However, I probably don't have your fonts.

Tricks like this can help:

Code: Select all

convert -pointsize 72 label:"\n  MR  \n" x.png
This puts a blank line above and below the text, and two spaces on both sides. This generally overcomes weirdness in the font metrics, where glyphs extend beyond their boundaries. Then trim and add a border:

Code: Select all

convert -pointsize 72 label:"\n  MR  \n" -trim -bordercolor white -border 5 x.png
snibgo's IM pages: im.snibgo.com

Sardoan
Posts: 10
Joined: 2017-10-17T10:57:05-07:00
Authentication code: 1151

Re: Center a text problem

Post by Sardoan » 2017-10-18T01:52:07-07:00

I am using ImageMagick from within an elixir-application. My code is:

Code: Select all

  defp create_picture(outfile, initials) do
    size = 480
    resolution = 72
    sampling_factor = 2
    color = ...some color...
    fontcolor = ...some color...
    System.cmd "convert", [
      "-density", "#{resolution * sampling_factor}",                        # sample up
      "-size", "#{size*sampling_factor}x#{size*sampling_factor}",   # corrected size
      "-background", "#{color}",                                                   # background color
      "-fill", "#{fontcolor}",                                                          # text color
      "-font", "Open-Sans",                                                           # font type
      "-gravity", "Center",                                                             # center text
      "caption: #{initials}",                                                           # text
      "-resample", "#{resolution}",                                                 # sample down to reduce aliasing
      outfile
    ]
  end
This is the actual code I am using. Tried already different approches with annotate, label, caption... -gravity center is making always the same mistake. The font is Open Sans, same result with Roboto.

And I don't want to use a fixed pointsize. Try "MR" with pointsize "72" and try it with "WW". You will see why I want to avoid the pointsize. The avatars are getting processed inside some applications and rendered as a circle. What I want is a simple picture with two letters and a padding. Pointsize auto and text centered. Seems not that difficult, but it seems to me ImageMagick can't do the job in a simple way, because gravity center cannot calculate the width of some letters correctly.

"\n MR \n":
Image

snibgo
Posts: 9382
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Center a text problem

Post by snibgo » 2017-10-18T03:19:50-07:00

What version of IM are you using?
snibgo's IM pages: im.snibgo.com

Sardoan
Posts: 10
Joined: 2017-10-17T10:57:05-07:00
Authentication code: 1151

Re: Center a text problem

Post by Sardoan » 2017-10-18T03:44:31-07:00

Version: ImageMagick 6.8.9-9 Q16

User avatar
fmw42
Posts: 22087
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Center a text problem

Post by fmw42 » 2017-10-18T10:00:17-07:00

Using IM 6.9.9.20 Q16 Mac OSX in command line, your equivalent commands work fine for me. Caption and label have been tweeted over time. So your old version 6.8.9.9, may have had bugs.

Code: Select all

convert -size 960x960 -background red -fill white -font OpenSans -gravity center caption:"WR" -density 144 -resample 72 WR.png 
Image

Code: Select all

convert -size 960x960 -background red -fill white -font OpenSans -gravity center caption:"WW" -density 144 -resample 72 WW.png 
Image

User avatar
fmw42
Posts: 22087
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Center a text problem

Post by fmw42 » 2017-10-18T15:10:42-07:00

I also tested IM 6.8.9.9 Q16 Mac OS X with the same commands as above and it worked fine -- same results as posted above.

snibgo
Posts: 9382
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Center a text problem

Post by snibgo » 2017-10-18T15:51:23-07:00

@Sardoan: your images have black borders. Nothing in your code would make any borders. So where have they come from?
snibgo's IM pages: im.snibgo.com

Sardoan
Posts: 10
Joined: 2017-10-17T10:57:05-07:00
Authentication code: 1151

Re: Center a text problem

Post by Sardoan » 2017-10-18T23:47:17-07:00

Seems I should try a newer version. Thanks. The borders are not from the picture itself. I have a docker container in a virtual machine and the pictures were screenshots from the atom editor and borders are there because I cutted them not too precisly.

Thanks a lot, hope the newer version does the job :)

Sardoan
Posts: 10
Joined: 2017-10-17T10:57:05-07:00
Authentication code: 1151

Re: Center a text problem

Post by Sardoan » 2017-10-19T01:31:36-07:00

:( Works with "WR". But not with "MR".

User avatar
fmw42
Posts: 22087
Joined: 2007-07-02T17:14:51-07:00
Location: Sunnyvale, California, USA

Re: Center a text problem

Post by fmw42 » 2017-10-19T09:57:57-07:00

Using IM 6.9.9.20 Q16 Mac OSX in command line, your equivalent commands work fine for me.

Code: Select all

convert -size 960x960 -background red -fill white -font OpenSans -gravity center caption:"MR" -density 144 -resample 72 MR.png 
Image

User avatar
GeeMack
Posts: 469
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Center a text problem

Post by GeeMack » 2017-10-19T10:14:38-07:00

Sardoan wrote:
2017-10-18T01:52:07-07:00
What I want is a simple picture with two letters and a padding. Pointsize auto and text centered. Seems not that difficult, but it seems to me ImageMagick can't do the job in a simple way, because gravity center cannot calculate the width of some letters correctly.
If you want an image of a particular size with text of some other particular size centered in the image, you may be making this more difficult than it needs to be. Keep in mind that every different font has "font metrics", which define the way various characters in the font behave relative to the printing space, base line, the other characters, etc. ImageMagick can probably do your job in a simple way, but of all the parts of your operation, the behavior of the font might be the last thing you should rely on to make it work.

For whatever it's worth, I might approach the task a little more like this...

Code: Select all

convert -background darkred -fill white -font "OpenSans-Regular.ttf" -pointsize 200 \
   label:"\n MR \n" -trim -resize 144x72 -gravity center -extent 480x480 output.png
Create the text in a size larger than you'll ever need it to be. Set the "-resize" arguments to the dimensions of the container you want the text to fit within. If your only concern is the height of the text, leave out the width setting like this... "-resize x72". Then set the "-extent" arguments to the output dimensions you want for the finished image. The result is almost exactly the same as auto-sizing your text to fill a container you define, then padding the text to the final output size you want with the text exactly centered.

Post Reply