Search code examples
imagemagickimagemagick-convert

Add Text in the center of the image using ImageMagick


I am trying to draw text on the center of the existing image but not able to achieve desired result:

Here is so far i have tried

convert src-image.png -gravity center -pointsize 144 -fill red -annotate -50 'REGISTERED' temp2.png

I am able to achieve text in the center at an angle, but my requirement is to have such text for multiple image dimensions which can vary.

  • Font size should be big enough to visible
  • Text should start from left bottom corner -> right top corner

Here is one of the reference i tried but cannot get exact command to work. Please let me know how can i achieve this following result for variable image dimensions. Expected image: enter image description here


Solution

  • I was able to achieve the desired result using following shell script reference from the linked answer:

    #!/bin/bash
    
    # Width, height and text
    w=$1
    h=$2
    text=$3
    source_file=$4
    target_file=$5
    
    # Get pointsize ImageMagick thinks is good
    pointsize=$(convert -gravity center -size ${w}x${h} caption:"$text" -format "%[caption:pointsize]" info:)
    
    echo ImageMagick likes pointsize: $pointsize
    
    # So draw text in that size on larger canvas, trim to bounds of letters and resize to desired size
    wb=$((w*2))
    hb=$((h*2))
    convert $source_file -gravity center -fill white -size ${wb}x${hb} -pointsize $pointsize -annotate -45 "$text" -trim +repage -resize ${w}x${h}\! $target_file
    
    

    Caller

    ./paint.sh 5237 7852 "REGISTERED" input-image.jpeg result.jpg