vertically centre annotated text

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Nik
Posts: 37
Joined: 2009-09-16T03:54:58-07:00
Authentication code: 8675309

vertically centre annotated text

Post 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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: vertically centre annotated text

Post 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?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: vertically centre annotated text

Post 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
Nik
Posts: 37
Joined: 2009-09-16T03:54:58-07:00
Authentication code: 8675309

Re: vertically centre annotated text

Post 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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: vertically centre annotated text

Post 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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: vertically centre annotated text

Post 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.
Nik
Posts: 37
Joined: 2009-09-16T03:54:58-07:00
Authentication code: 8675309

Re: vertically centre annotated text

Post 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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: vertically centre annotated text

Post by Bonzo »

To confirm you want the caption on the top and bottom of the image or just the bottom instead of the top?
Nik
Posts: 37
Joined: 2009-09-16T03:54:58-07:00
Authentication code: 8675309

Re: vertically centre annotated text

Post 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
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: vertically centre annotated text

Post 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
Nik
Posts: 37
Joined: 2009-09-16T03:54:58-07:00
Authentication code: 8675309

Re: vertically centre annotated text

Post by Nik »

Thanks for your help. I'll pick this up in the morning and post what I come up with.

Thanks,
Nik
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: vertically centre annotated text

Post 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.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: vertically centre annotated text

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: vertically centre annotated text

Post 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:
Nik
Posts: 37
Joined: 2009-09-16T03:54:58-07:00
Authentication code: 8675309

Re: vertically centre annotated text

Post 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
Post Reply