create box to fit caption rather than other way around

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
wfzimmerman
Posts: 8
Joined: 2013-04-17T11:31:14-07:00
Authentication code: 6789

create box to fit caption rather than other way around

Post by wfzimmerman »

Hi,

I am trying to figure out how to create a box of the necessary size to underlay a caption of arbitrary length. As I understand it, that is the opposite of how caption normally works so am a bit stumped.

my requirements are as follows:

image of arbitrary size (typically around 1200 x 800)
caption of arbitrary length, white type on solid color background, laid on top of bottom of image, typically one or two lines high x at most image width
caption can be arbitrary font
fontsize is 11
if caption height exceeds X% of image height, caption is too long, so report error forcing user to modify caption

in other words, I need to create a background box that is just big enough to hold the caption.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: create box to fit caption rather than other way around

Post by fmw42 »

I am not sure I understand. But create your caption image on a transparent background to a given width (but skip height) and given pointsize. Then check the dimensions. If appropriate, composite it at the locations you want on your background image.

convert -size 600x -background none -pointsize 50 -font arial -gravity center caption:"This Is Some Long Text To Test Whether It Wraps" result.png
height=`convert result.png -format "%h" info:`
echo $height
171

So all you need to do is add an if condition and if acceptable height, then composite it over your background

convert background result.png -gravity XX -geometry +X+Y -compose over -composite final.png

see
http://www.imagemagick.org/Usage/layers/#convert
http://www.imagemagick.org/Usage/text/#caption
Post Reply