Page 1 of 2

Image size for font size

Posted: 2008-08-21T14:34:23-07:00
by vadimiron
I hove some text, a font and size of the font. Now i need to put this text on the white rectangle (or simply square), but it's important that this rectangle is filled out by the text with no white spaces on sides. For that i need to calculate the widht and height of the image depending on the font and font size.
Can imagemagick do it automatically (on command line)????

Re: Image size for font size

Posted: 2008-08-21T15:05:40-07:00
by fmw42

Re: Image size for font size

Posted: 2008-08-21T17:08:09-07:00
by anthony
label: by default (no -size setting) sizes the image to fit the text, font and pointsize.

If you leave out the -pointsize but set -size instead, IM resizes the text to best fit that size.

It is all explained in the link fmw42 gave.

Re: Image size for font size

Posted: 2008-08-21T23:42:08-07:00
by vadimiron
anthony wrote:label: by default (no -size setting) sizes the image to fit the text, font and pointsize.
Thank you guys!!!

But another one question: OK the image will be resized to fit the text, but can i influence the ratio of sides? For example, i would like an image with 4:3 ratio (or just square) and that fits to the text - is it possible?? I ask just, because it's maybe possible, that an image for one sentence will be generated as a long line (width: 1000px, heigt: pointsize) - is it possible?? Or i don't have to think about that and imagemagick generates just squares per default???

Re: Image size for font size

Posted: 2008-08-22T09:26:55-07:00
by fmw42
I don't know any way to get the aspect ratio as desired, except to use -resize afterwards, but that would distort the text.

Note that caption will draw text to multiple line.

Re: Image size for font size

Posted: 2008-08-23T10:27:58-07:00
by vadimiron
Great!
Everything works!!!!

Last one question: Can i influence the line heigth (margin between lines) and char-spacing??

Re: Image size for font size

Posted: 2008-08-23T10:52:26-07:00
by fmw42
Not that I am aware (other than by choice of font) in MVG, but see the specs at http://www.imagemagick.org/script/magic ... aphics.php

also see if SVG is more forgiving at http://www.w3.org/TR/SVG/index.html

read Anthony's examples at http://www.imagemagick.org/Usage/draw/

Re: Image size for font size

Posted: 2008-08-23T11:51:00-07:00
by vadimiron
Or PostScript - it could be used, too...I suppose

But, i think, i will choose MVG or SVG

Thanks!

Re: Image size for font size

Posted: 2008-08-24T16:04:19-07:00
by anthony
vadimiron wrote:i would like an image with 4:3 ratio (or just square) and that fits to the text - is it possible??
Not simply.

The best way is to set a large image -size that is 4:3 ratio, use caption: to word wrap the given text into an image of that size (without a -pointsize setting. Then resize it to the size needed.

If you know the final size in advanced, you could just generate it directly.

Re: Image size for font size

Posted: 2008-08-28T10:36:34-07:00
by stevepugh
Hi all,

This looked like a good post to hijack as I have a related question (and the original poster might benefit from my angst ;-)

I am using label: to generate six or seven blocks of text for a slate generator, and the variables being populated look great - the date, artist, scene, etc. are all left-justified and evenly spaced, which is cool.

The actual 'descriptors', though ("Date:", "Artist:", "Shot No.:", etc.) are variable length and they want to be right-justified so that the slates look as pretty as possible. Of *course* I want my users to be able to make slates that read "Shot:" or "Scene Name:" depending on preference, so now I have to figure out how to make it look nice with a variety of text widths.

Presently these captions are written with image stacks thusly:

Code: Select all

 ( -background transparent -fill White -stroke grey -font Arial -pointsize 40 -size x70 -gravity east label:Date: ) -geometry +200+500 -compose over -composite  ( -background transparent -fill White -stroke grey -font Arial -pointsize 40 -size x70 -gravity east label:Artist Name: ) -geometry +200+600 -compose over -composite 
So far so good.

My question, how can I capture the width of the resulting image, so that I can offset the -geometry +200 so that they all line up along their rightmost edges? If I can get that width on the same commandline, I'll be grinning like...well, like a grinning thing.

Many thanks,
Steve

Re: Image size for font size

Posted: 2008-08-28T11:24:41-07:00
by dognose
Steve, I've been working w/ IM for a long time and have not found any good ways to get font metrics... at least with the command line.

I believe there is a way in the magic++, but, I don't use that.

There are so many uses I could have if there was an easy way to get the size and spacing of the fonts.

Re: Image size for font size

Posted: 2008-08-28T12:00:40-07:00
by stevepugh
Right on, I poked around the docs enough to figure that actual font metrics were outside the realm of the feasible.

However, I'm still hopeful that I can deduce the width of the "stack" image generated by ( -size x70 -gravity east label:"Artist Name: " ) and calculate based on that.

I was initially thinking I could draw the label with a fixed pointsize in a box wide enough to cover any text, and then use that width to calculate my offset (make an 800-pixel wide label with right-justified text and composite it at a -200 x offset so that all of the right edges line up at 600 pixels), but the -compose operation apparently doesn't respect the -pointsize parameter, so there goes that.

Ever onward, ever upward!
Steve

Re: Image size for font size

Posted: 2008-08-28T12:19:24-07:00
by magick
ImageMagick reports the font metrics when you use the -debug annotate option. The patch will be available sometime tomorrow from the ImageMagick subversion trunk.

Re: Image size for font size

Posted: 2008-08-28T12:35:37-07:00
by fmw42
It is kind of long, but have you tried

... -geometry $(+clone -format %w info:)x$(+clone -format %h info:)+200+500 ...


I have not tested it.

But if possible for IM to allow more use of fx escapes and string formats as arguments (perhaps in a simpler way) that would be great. If it is already available, I would appreciate further information.

I look forward to the -font-metric in any case.

Re: Image size for font size

Posted: 2008-08-28T13:08:49-07:00
by stevepugh
I'm intrigued by and looking forward to the font metrics availability - would that allow me to get the width in pixels of my text strings for nefarious purposes like this? Yay!

fmw42, I dropped your code right into my commandline and am trying to get it to work (I suspect I need to escape the '%' characters on my Windows machine), but I'll keep on it. Can you tell me more about what that line *ought* to do? I can see where -format %w info: might give me the width of the text image, but I'm not sure what it's doing otherwise :-)

Many thanks again,
Steve