Expand canvas to fit text?

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
ggking7
Posts: 21
Joined: 2010-11-15T13:19:04-07:00
Authentication code: 8675308

Expand canvas to fit text?

Post by ggking7 »

Can I have the canvas automatically expand or contract horizontally on the right side to fit the width of the text in the following example?

convert -size 98x28 xc:"black" -font Verdana -pointsize 14 -fill black -draw "text 30,20 'test'" -bordercolor black -border 1x1 -fill white -draw 'color 0,0 point' -draw 'color 99,0 point' -draw 'color 0,29 point' -draw 'color 99,29 point' background.png

I've been using the following command to place another image over than generated by the above command. Can I combine these commands or is there another option besides writing a temporary background.png image?

convert -composite background.png foreground.png -geometry +10+5 image.jpg
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Expand canvas to fit text?

Post by fmw42 »

To make the canvas expand, you need to use label: or caption: Use a transparent background and then overlay the text onto your other image.

see
http://www.imagemagick.org/Usage/text/#label_bestfit
http://www.imagemagick.org/Usage/text/#caption_bestfit

best to review the whole text page referenced
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Expand canvas to fit text?

Post by anthony »

There is also more notes about getting the canvas size needed for some text (for other purposes, like tiling) in a later section...

IM Examples, Text to Image Handling, Automatically Sized Annotated Text Canvases
http://www.imagemagick.org/Usage/text/#annotate_size
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
ggking7
Posts: 21
Joined: 2010-11-15T13:19:04-07:00
Authentication code: 8675308

Re: Expand canvas to fit text?

Post by ggking7 »

Thanks for those links. I can see how to size the canvas around some text. How can I leave enough empty space only on the left side of the canvas in order to place an image there? I'm currently placing the image with the second line in my original post.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Expand canvas to fit text?

Post by anthony »

You can simply append a blank canvas of the appropriately size on the left hand side!
or just append the image you want !

See IM examples, Layers of Images, Append
http://www.imagemagick.org/Usage/layers/#append

You may need +swap to put the image (or canvas) before the generated label text.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
ggking7
Posts: 21
Joined: 2010-11-15T13:19:04-07:00
Authentication code: 8675308

Re: Expand canvas to fit text?

Post by ggking7 »

Too easy! It's working great.

How can I add a white dot to each corner? I was using -draw in the code I posted but I no longer know the size of the canvas since it's trimmed around the text.

Am I correct that the two links which fmw42 posted are for resizing text to a canvas as opposed to resizing a canvas to text?

I can't figure out how the -annote numbers work (although +20+80 seems to be good) and the description online is pretty complex. Can you give me the super-abridged version in layman's terms so I have some idea what's going on there?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Expand canvas to fit text?

Post by fmw42 »

the +20+80 are the offsets from the -gravity setting. So if -gravity center, then the "center of the text" will be moved to that offset from the center of the image. If no -gravity is specified then the top left corner of the text will be offset relative to the upper left corner of the image (equivalent to -gravity northwest)

My links were to best fit the text to some specified image size.

If you have an image and want dots (one pixel wide) in the corners, say black on white, then

convert whiteimage \( -size 1x1 xc:black \) -gravity northwest -compose over -composite \
\( -size 1x1 xc:black \) -gravity southwest -compose over -composite \
\( -size 1x1 xc:black \) -gravity southeast -compose over -composite \
\( -size 1x1 xc:black \) -gravity northeast -compose over -composite \
result
ggking7
Posts: 21
Joined: 2010-11-15T13:19:04-07:00
Authentication code: 8675308

Re: Expand canvas to fit text?

Post by ggking7 »

the +20+80 are the offsets from the -gravity setting. So if -gravity center, then the "center of the text" will be moved to that offset from the center of the image. If no -gravity is specified then the top left corner of the text will be offset relative to the upper left corner of the image (equivalent to -gravity northwest)
Adding -gravity center allowed me to use -annotate 0 'text'.
If you have an image and want dots (one pixel wide) in the corners, say black on white, then

convert whiteimage \( -size 1x1 xc:black \) -gravity northwest -compose over -composite \
\( -size 1x1 xc:black \) -gravity southwest -compose over -composite \
\( -size 1x1 xc:black \) -gravity southeast -compose over -composite \
\( -size 1x1 xc:black \) -gravity northeast -compose over -composite \
result
Works great! Thank you both for your help!
Post Reply