Caption -gravity is not weighty

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?".
Post Reply
newbury
Posts: 4
Joined: 2012-11-22T09:57:11-07:00
Authentication code: 6789

Caption -gravity is not weighty

Post by newbury »

I think I must have the magick incantation wrong. Using Fedora 17 and convert Version: ImageMagick 6.7.5-6 2012-08-11 Q16.
I want to use caption -gravity center to center a long text in an image with word wrap. What I *ALWAYS* get is efectively northwest, left justified.
Using label with -gravity center, I get centered text, but it wraps off both sides.
Here is the code, including setting up the example.

Code: Select all

#!/bin/bash
#Code:
file=temp/somefile
line1=`echo "By default the text is all left justified, however as of IM version 6.2.0, 'caption:' respects '-gravity' for text justification purposes."`
line2=`echo "NOT!"`
line3=`echo "Output is North L-J"`
echo $line1  > "${file}"
echo $line2 >> "${file}"
echo $line3 >> "${file}"
title=$file
#  foregoing recreates script structure creating the text to be imaged
# create blank white background just for this example
    convert -background white -font Liberation-Serif-Regular -pointsize 1 -fill black -size 800x340 -gravity south label:"." label0.png
# first version: using label
    convert -background black -transparent black -font Liberation-Serif-Regular -pointsize 34 -fill "dark blue" -size 800x340 -gravity center label:@"${title}" label1.png
    composite -geometry -10+50  label1.png label0.png -blend 99 label2.png
# works FINE, except that it overwraps the long line!

# second version: using caption
    convert -background black -transparent black -font Liberation-Serif-Regular -pointsize 34 -fill "dark blue" -size 800x340 -gravity center caption:@"${title}" label3.png
    composite -geometry -10+50  label3.png label0.png -blend 99 label4.png
So what is wrong with this picture?? Is there some interaction between the use of pointsize AND image size?? I do need a fairly large pointsize in order that the text be readable..
Any and all help appreciated.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Caption -gravity is not weighty

Post by fmw42 »

I am no expert on this, but there have been many tweaks to label: and caption: over time to fix things like this. It seems that sometimes one fix cause a different issue with just some specific combination of font, pointsize and width. Check the changelog at http://www.imagemagick.org/script/changelog.php. Perhaps a newer (or older) release will work for your example. Otherwise, I would suggest installing PANGO and trying that. See http://www.imagemagick.org/Usage/text/#pango

P.S. Do not specify both point size and both image width and height if you want the text to adjust the pointsize to fill the image.

You can also try -annotate, if you know the pointsize that will fill the image.

This works fine for me on IM 6.8.0.5 Q16 Mac OSX Snow Leopard

# no pointsize
convert -background lightblue -fill blue -font Arial -size 320x140 \
-gravity center caption:'Now Is The Time For All Good Men To Come To The Aid Of Their Country.' \
1tmp1.gif

# Pointsize and width
convert -background lightblue -fill blue -font Arial -size 320x -pointsize 36 \
-gravity center caption:'Now Is The Time For All Good Men To Come To The Aid Of Their Country.' \
1tmp2.gif
newbury
Posts: 4
Joined: 2012-11-22T09:57:11-07:00
Authentication code: 6789

Re: Caption -gravity is not weighty

Post by newbury »

Thanks for that.
The 6.7 version is the native Fedora install version. I shall grab and install the newer 6.8.
With luck that might fix things.
I am creating an actual title page, so the width is fixed.
That also means that I REALLY want a visible pointsize. It does me no good to have the program shrink things to 8 point in order to fit into the width.

Oh wow: conflicting restraints. Never seen that before </sarc>.

Annotate will not work as it does not recognize formatting codes (so \n or ever \\n are ignored.
What I have been doing is using about a hundred lines of code to parse the title into junks of one line length each, and using label: to deal with each line, seriatim. It works, kinda ok.
The problem now is that I need to deal with 2 long lines (the first and third) and the three lines need to be stacked neatly.. so the complexity just went through the roof.

Now if annotate only recognized formatting codes!

convert will do want I want, *except* for centering the lines of text...
Any 'out-of-the-box' ideas which would allow me to do indirectly, what I cannot do directly?
For example: can the output text stream of a 'convert caption' be re-directed to a file instead of overlaid on the image (does it exist in a set of internal variables which can be parsed???)
newbury
Posts: 4
Joined: 2012-11-22T09:57:11-07:00
Authentication code: 6789

Re: Caption -gravity is not weighty

Post by newbury »

Guess I need to find the 6.8 version.
None of the suggestions worked with caption -gravity center!

Removing the pointsize item, gave me the first line, on one line, in 8point or so, AND left justified.

Replacing pointsize and removing either the 800 or the 340 or BOTH of the size items gave me left justified title. The point size appears to be slightly differ in those three cases, but none are workable..
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Caption -gravity is not weighty

Post by Bonzo »

Here is a link to some old wordwrap tests I did and at the time -annotate supported \\n
http://www.rubblewebs.co.uk/imagemagick/notes/wrap.php
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Caption -gravity is not weighty

Post by fmw42 »

You can specify the desired width and pointsize as I did in the second example. The issue, I think, is that you may get conflicts with both widthxheight and pointsize.

I had not trouble with centering. You can also make line breaks with \n as in the example at http://www.imagemagick.org/Usage/text/# ... paragraphs


I am not sure what you mean by -annotate does not take formatting? Annotate does not know how to word wrap, if that is what you mean.

Note that -gravity center in caption: is not like justifying. With -gravity center each wrapped line will be centered and you may get uneven line widths.

See http://www.imagemagick.org/Usage/text/#pango for proper justifying text.
newbury
Posts: 4
Joined: 2012-11-22T09:57:11-07:00
Authentication code: 6789

Re: Caption -gravity is not weighty [SOLVED]

Post by newbury »

The upgrade to 6.8.05 did the trick.
I can now use this to produce a transparent overlay image with text in dark blue, text fed from a file:
convert -background black -transparent black -pointsize 34 -fill "dark blue" -size 800x340 -gravity center caption:@"${title}" label3.png

and get the input lines (three in this case) , each sliced into proper size lengths, each individually centered horizontally and the stack of lines (anywhere from 3 to 7 lines) centered vertically in the image box.

Success! Thank you devs!. EXACTLY WHAT I NEEDED. It obsoletes about a hundred lines of code!

@fwm42 Re: annotate: We're talking about almost the same thing. Annotate does not word wrap. It also does not recognize things like '\n' which could be (otherwise) used to make a fake word wrap. And that's basically what my soon-to-be-gone hundred lines did. It made individually tailored lines and added them to a file, to be fed to label:@. Problem *was* that there was a lot of complexity in dealing with the variable line lengths AND the resulting variable number of label lines AND attempting to get that properly placed.
Now a one-liner!!! MADE my day!
Post Reply