Page 1 of 1
Expand canvas to fit text?
Posted: 2013-05-07T19:48:41-07:00
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
Re: Expand canvas to fit text?
Posted: 2013-05-07T20:06:15-07:00
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
Re: Expand canvas to fit text?
Posted: 2013-05-07T21:59:15-07:00
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
Re: Expand canvas to fit text?
Posted: 2013-05-09T23:13:29-07:00
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.
Re: Expand canvas to fit text?
Posted: 2013-05-09T23:36:41-07:00
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.
Re: Expand canvas to fit text?
Posted: 2013-05-10T18:00:32-07:00
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?
Re: Expand canvas to fit text?
Posted: 2013-05-10T18:11:31-07:00
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
Re: Expand canvas to fit text?
Posted: 2013-05-11T14:06:52-07:00
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!