On Mon, 2009-11-30 at 17:46 +0000, Mark Rogers wrote:
I'm playing with ImageMagick's "convert" program to add text to an image.
I have something like this (which works): convert "infile.png" -font "font.ttf" -gravity 'south' \ -stroke '#fffc' -strokewidth '3' -pointsize '300' \ -annotate 0 "Test" "outfile.png"
However, the only way I can find to size the text is using the -pointsize option. What I want to do is make the text proportional to the image size, eg have the text work out to 50% of the available width, or similar. I can't find any good way to do this - and nor can I find a suitable query for Google to assist me.
If the text is always the same you could experiment manually with one image until you find the proportion you are looking for then divide the image width by the point size you finally settled on to arrive at a pixels/point factor. For each image you do automatically you derive the point size by dividing the width of this particular image by the factor.
If you are using a fixed width font but variable text you could work out the width in pixels for one character at one font size and scale accordingly for the text and width of the images you are working with.
For variable text in a proportional width font you could see if you can get access to the font metric information via one of the libraries that handles true-type fonts, i.e. where you can ask the library "If I draw this text at this point size, how wide will it be?" then by successive approximation refine the point size to get the width you want. I know of FreeType as one such library but there may be others.
HTH, Steve.