Page 1 of 2

vertically centre annotated text

Posted: 2015-08-16T12:09:46-07:00
by Nik
Hi all,

is there an option or calculation to vertically centre annotated text?

Here is my command:

Code: Select all

/opt/local/bin/convert input.jpg -gravity North -background '#800000' -splice 0x30 -font 'AvantGarde-Book' -fill '#FFAEB9' -pointsize '18' -annotate +0+3 'this is my text' output.jpg
The thing to bare in mind is that the font style and point size is variable along with the size of the block of colour!!!

I use a version of IM that's been installed using macports:

Version: ImageMagick 6.8.9-8 Q16 x86_64 2014-10-21 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules
Delegates: bzlib djvu fftw fontconfig freetype gslib jbig jng jp2 jpeg lcms ltdl lzma openexr png ps tiff webp x xml zlib

Many Thanks,
Nik

Re: vertically centre annotated text

Posted: 2015-08-16T12:46:27-07:00
by Bonzo
I think to do what you want you need to make the text area separate from the image and -append it.
This has a drawback as you need to find the width of the original image but I seem to remember seeing a trick to do it. I wonder if it had something to do with cloning the original image filling it with the colour and then cropping? A bit long winded but it could work?

Re: vertically centre annotated text

Posted: 2015-08-16T12:56:14-07:00
by Bonzo
I was thinking something along this line would work but :(

Code: Select all

convert input.jpg ( -clone 0 -fill "#FFAEB9" -crop x30+0+0 -gravity center -pointsize 18 -annotate +0+3 "this is my text" ) -append output.jpg
Close but still no coconut ( still had to hard code in the crop width ):

Code: Select all

convert input.jpg -fill "#FFAEB9" -crop 100x30+0+0 -gravity center -pointsize 18 -annotate +0+3 "this is my text" input.jpg -append output.jpg

Re: vertically centre annotated text

Posted: 2015-08-16T13:02:52-07:00
by Nik
Thanks for your reply.

I wander if it would be simpler to create a caption label and then add it to the top of the original image?

Code: Select all

convert -background '#800000' -fill '#FFAEB9' -font Arial -pointsize 18 -size 300x30 -gravity Center caption:'this is my text' output.jpg
as this seems to centre the text horizontally and vertically. As you pointed out I need to know the width of the original image which thankfully I do. The only part I don't know how to do is how to add the caption to the top of the original image?

Thanks,
Nik

Re: vertically centre annotated text

Posted: 2015-08-16T13:11:26-07:00
by Bonzo
That is quite simple:

Code: Select all

convert -background '#800000' -fill '#FFAEB9' -font Arial -pointsize 18 -size 300x30 -gravity Center caption:'this is my text' input.jpg -appened output.jpg

Re: vertically centre annotated text

Posted: 2015-08-16T13:15:35-07:00
by Bonzo
Most of my code is written for php and I use getimagesize() and use the width variable in the convert code. You can get the image width using the fx option in Imagemagick but that is an extra step as well.
If you are running a script it is not really a problem but some people want everything on one line. I believe in version 7 you should be able to get the image width on the same line of code as the convert.

Re: vertically centre annotated text

Posted: 2015-08-16T13:25:46-07:00
by Nik
That works great.

Is it possible to also append to the bottom of the input image? maybe -gravity South or something?

Thanks,
Nik

Re: vertically centre annotated text

Posted: 2015-08-16T13:29:08-07:00
by Bonzo
To confirm you want the caption on the top and bottom of the image or just the bottom instead of the top?

Re: vertically centre annotated text

Posted: 2015-08-16T13:33:52-07:00
by Nik
I would like to be able to add a caption to the top or bottom or both, depending on what the user inputs.

Thanks,
Nik

Re: vertically centre annotated text

Posted: 2015-08-16T13:52:00-07:00
by Bonzo
The order of the images defines whether it is at the top or bottom. This will give you both:

Code: Select all

convert -background "#800000" -fill "#FFAEB9" -font Arial -pointsize 18 -size 300x30 -gravity Center caption:"this is my text" -write mpr:image +delete mpr:image -append input.jpg mpr:image -append output.jpg

Out of interest this variation of the auto width nearly works but I can not fill the caption area with a colour ( hopefully fmw42 or snibgo can make a suggestion on how to do that):

Code: Select all

convert ( input.jpg -resize x30! -fill "#800000" -gravity center -fill "#FFAEB9" -pointsize 18 -annotate +0+3 "this is my text" ) input.jpg -append output.jpg

Re: vertically centre annotated text

Posted: 2015-08-16T13:54:02-07:00
by Nik
Thanks for your help. I'll pick this up in the morning and post what I come up with.

Thanks,
Nik

Re: vertically centre annotated text

Posted: 2015-08-16T13:55:40-07:00
by Bonzo
I should explain the first piece of code above:
The caption image is created and saved into the memory called image; that image is then called from the memory twice and use in the append option.

Re: vertically centre annotated text

Posted: 2015-08-16T14:04:50-07:00
by Bonzo
Thanks to Anthony this works for the original question background colour problem and no need to specify the caption width:

Code: Select all

convert input.jpg -resize x30! -fill "#800000" -draw "color 0,0 reset" -gravity center -fill "#FFAEB9" -pointsize 18 -annotate +0+0 "this is my text" -write mpr:image +delete mpr:image -append input.jpg mpr:image -append output.jpg
The only problem is I am still reading the input image twice

Re: vertically centre annotated text

Posted: 2015-08-16T17:04:14-07:00
by fmw42
Bonzo, your version seems to be missing the input image in the middle of the two copies of the text.

Is this what is wanted?

Code: Select all

convert input.jpg -write mpr:orig +delete \
\( mpr:orig -resize x30! -fill "#800000" -colorize 100% \
-gravity center -fill "#FFAEB9" -pointsize 18 -annotate +0+0 "this is my text" \
-write mpr:text +delete \) \
mpr:text mpr:orig mpr:text -append output.jpg
or a little shorter code as

Code: Select all

convert logo: -write mpr:orig \
\( mpr:orig -resize x30! -fill "#800000" -colorize 100% \
-gravity center -fill "#FFAEB9" -pointsize 18 -annotate +0+0 "this is my text" \
-write mpr:text \) \
+swap mpr:text -append show:

Re: vertically centre annotated text

Posted: 2015-08-17T06:35:27-07:00
by Nik
Hi all,

I realise that this can probably be done more efficiently but I've chosen to create a file for each of the captions and then stitch them back together using the -append & +append options. This gives me the flexibility to put the captions on any of the four edges as I could need to put them on the left or right of an image as well now. I wasn't aware that an image could be held in memory so it's been a good learning curve.

Thanks for all the help,
Nik