Page 1 of 1

Autosize text based on label size, not image size

Posted: 2011-06-21T19:50:28-07:00
by Kynlyn
I'm trying to use convert to create a 640x480 transparent PNG, with a "header" at the top of the image that is 640x50 that will contain text that will autosize to best fit within the 640x50 size. Can this be done with a single call to convert, or do I need to use compose? In the end, I need to create the larger PNG with multiple areas of text that have to be autosized. (ie, a header line of text, a center line of text and a footer.) I don't want the text to be wrapped, but rather autofit to the height specified.

I can easily create a transparent PNG of 640x480; I can easily create 640x50 image that contains text that has been autosized, but I can't figure out how to do this into one call to convert. I've tried various incantations, but here is the latest:

Code: Select all

convert -size 640x480 -background none -repage 640x50 label:'Header Txt' image.png
My thoughts were that this would create the image as a 640x480, but change the canvas to 640x50 for the label. However, my -repage seems to have no effect as the text is resized for the entire 640x480, not the smaller 640x50.

What am I missing?

Re: Autosize text based on label size, not image size

Posted: 2011-06-21T19:57:29-07:00
by fmw42
I don't think you can do it as you are trying. I don't believe -repage will help here.

so try this:

convert \( -size 640x480 xc:white \) \
\( -size 640x50 -background none -gravity center label:'Header Txt' \) \
-gravity north -compose over -composite test.png

if on windows, see http://www.imagemagick.org/Usage/windows/

see parenthesis processing at http://www.imagemagick.org/Usage/basics/#parenthesis

Re: Autosize text based on label size, not image size

Posted: 2011-06-21T20:21:55-07:00
by Kynlyn
Perfect! I didn't even think about parenthesis. I'm going to go read up on how you did the compose and composite bits, but that works perfectly. Thank you very, very much!

Re: Autosize text based on label size, not image size

Posted: 2011-06-21T21:09:56-07:00
by fmw42
Kynlyn wrote:Perfect! I didn't even think about parenthesis. I'm going to go read up on how you did the compose and composite bits, but that works perfectly. Thank you very, very much!
You can continue to add more compose ... composite images in sequence to build up your final image. Also note that you can offset relative to -gravity using -geometry.

see
http://www.imagemagick.org/Usage/layers/#convert

or create all the images you need for text and then -flatten them or even -mosaic them.

see

http://www.imagemagick.org/Usage/layers/#flatten
http://www.imagemagick.org/Usage/layers/#mosaic
http://www.imagemagick.org/Usage/layers/#merge