hw to offset text from the top of an image and give it a text alignment of center

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
dt59
Posts: 49
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 » 2017-08-28T00:32:08-07:00

hello fred, did u test the code. am having undesired result,check pls

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

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by fmw42 » 2017-08-28T10:40:29-07:00

When in doubt, separate the commands and look at each intermediate result to see what is happening.

Code: Select all

convert -background white -fill black -gravity center -pointsize 36 label:"my first text" -trim +repage tmp1a.png
convert -background white -fill black -gravity center -pointsize 36 label:"center text" -trim +repage tmp2a.png
convert tmp1a.png tmp2a.png -gravity center -smush +20 -bordercolor white -border 10 tmp3a.png
convert tmp3a.png -bordercolor red -border 10 tmp3b.png
convert -size 500x500 xc:white tmp3b.png -geometry +57+146 -compose over -composite text_resultA.png
convert tmp3a.png -format "%wx%h" info:
202x99
Image

In the above, I found the size of the text image before adding the red border. So I cropped it a bit smaller in width in the following.

Code: Select all

convert -background white -fill black -gravity center -pointsize 36 label:"my first text" -trim +repage tmp1a.png
convert -background white -fill black -gravity center -pointsize 36 label:"center text" -trim +repage tmp2a.png
convert tmp1a.png tmp2a.png -gravity center -smush +20 -bordercolor white -border 10 tmp3a.png
convert tmp3a.png -gravity northwest -crop 184x99+0+0 +repage -bordercolor red -border 10 tmp3b.png
convert -size 500x500 xc:white tmp3b.png -geometry +57+146 -compose over -composite text_resultB.png
Image


The above is likely not what you want as I mentioned above in my previous email. And as I said before, there is no easy way that I know to do what you want. Imagemagick cannot do both wrap and trim text that is too long.

So what I think you have to do is write the text with -annotate or -draw and place each line where you want on an image of the size you want for your box before adding the border. Then add the border. Then composite image in your 500x500 white background in the place you want. This is what I said in my last email message.

Please provide an example of the way you want your result to look.

dt59
Posts: 49
Joined: 2017-07-25T23:57:06-07:00
Authentication code: 1151

Re: hw to offset text from the top of an image and give it a text alignment of center

Post by dt59 » 2017-08-29T00:11:18-07:00

thanks Fred, is now working perfectly well as i expected. Am still learning anyway

Post Reply