Autosize text based on label size, not image size

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
Kynlyn
Posts: 2
Joined: 2011-06-21T19:27:15-07:00
Authentication code: 8675308

Autosize text based on label size, not image size

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

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

Post 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
Kynlyn
Posts: 2
Joined: 2011-06-21T19:27:15-07:00
Authentication code: 8675308

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

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

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

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