56

I want to add text on an image at specific point and want it be centre aligned. How can I specify to margin from top? I want to specify margin in pixels/inches from top.

Currently I am using this command:

 convert temp.jpg -gravity Center  -pointsize 30 -annotate 0 'Love you mom' temp1.jpg 

it is writing text in centre of image. I want text to move to top.

This is what I am getting: enter image description here

This is what i want: enter image description here

4 Answers 4

54

Try using -gravity North (this will move your text to the top of the image) and then adding an offset (-annotate +0+100) to move down your text:

convert temp.jpg -gravity North -pointsize 30 -annotate +0+100 'Love you mom' temp1.jpg 
23

Instead, you can use -draw to specify the location you want your text.

For example:

convert -font helvetica -fill white -pointsize 60 -gravity center -draw "text 0,300 'TEXT TO BE DISPLAYED'" /image_address/Image_input.png /image_address/Image_output.png 

In this case, 0 is de x coordinate and 300 is the y coordinate, however the x coordinate is not really important since the -gravity center is already there.

Remember that the coordinate 0,0 is the top left corner.

1
  • 6
    Worth noting that -font <param> can take the full path to a .ttf file. Apr 4, 2020 at 21:51
10
convert -pointsize 40 -fill blue -draw 'text 600,600 "Love You Mom"' temp1.jpg temp2.jpg
                                              ^   ^
                                              |   |
                                        From Left |
                                                From Top
2
  • 5
    Welcome to Stack Overflow! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
    – rizerphe
    Jun 25, 2020 at 11:28
  • 1
    The only straight to the point answer. Could even compromise to -draw 'text X, Y "Love You, Mom"'
    – Paul
    Jan 9, 2022 at 23:28
0

There are a few ways to add text on an image. Here is the answer using Magick.NET using C#:

  1. Using the Annotate() function:

    image.Annotate(someText, new MagickGeometry(x, y, width, height), Gravity.Center)
    
  2. Using a combination of Composite function and new MagickImage("caption:...") functionality. You can basically overlay an image on top of another image. You can create a text caption image which will be placed on top of the original image:

    MagickReadSettings readSettings = new MagickReadSettings()
    {
       Font = someFont,
       Width = 868,
       Height = 180,
       TextGravity = Gravity.North
    };
    using (var overlayImage = new MagickImage("caption:" + someText, readSettings))
    {
        originalImage.Composite(overlayImage, Gravity.South, x, y);
    }
    
  3. Using DrawableText you can draw text anywhere on the image:

    DrawableTextAlignment centerAligned = new DrawableTextAlignment(TextAlignment.Center);
    DrawableText drawableText = new DrawableText(x, y, someText);
    image.Draw(drawableText, centerAligned);
    
New contributor
Simcha Khabinsky is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.