1

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

2 Answers 2

1

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
1

This ImageMagick command is in Windows CLI syntax, and uses IMv7 emulating v6 with "convert"...

magick convert input.png ^
  -background none -bordercolor none -size 1000x1000 -fill red -gravity center ^
   ( label:REGISTERED -rotate -45 -trim -border 10% +repage ) ^
   -set option:distort:viewport "%[fx:u.w]x%[fx:u.h]" ^
   -distort SRT "%[fx:s.w/2],%[fx:s.h/2] %[fx:t?min(u.w,u.h)/min(v.w,v.h):1] 0 %[fx:u.w/2],%[fx:u.h/2]" ^
   -composite result.png

The command first reads in the input image. Then in parentheses it creates a label containing the text on a transparent background, in a size larger than your largest expected input image. It rotates the label 45 degrees, trims it, and adds a transparent border to increase the dimensions by 10%.

After the parentheses it sets a viewport size to the dimensions of the input image. Then using a -distort SRT operation, it resizes the label to fit inside the dimensions of the input image without affecting the size of input image.

After resizing the label, it is simply composited centered onto the input image.

This should accept nearly any size input image, and create and size a label as necessary to fit. Adjust the border width to provide more or less free space around the label.

To run this with IMv6, omit "magick" from the beginning of the command. To make it work on a *nix OS, change all the continued-line carets "^" to backslashes "\", and escape the parentheses around the "label" line with backslashes "\(...\)". I haven't tested it on a *nix shell, but that should work to convert the command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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