Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial (non-profit) use, ONLY.

    For use of my scripts in commercial (for-profit) environments or non-free applications, please contact me (Fred Weinhaus) for licensing arrangements. My email address is fmw at alink dot net.

    If you: 1) redistribute, 2) incorporate any of these scripts into other free applications or 3) reprogram them in another scripting language, then you must contact me for permission, especially if the result might be used in a commercial or for-profit environment.

    Usage, whether stated or not in the script, is restricted to the above licensing arrangements. It is also subject, in a subordinate manner, to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

    Please read the Pointers For Use on my home page to properly install and customize my scripts.

AUTOLABEL


Places text automatically in a specified size region that best matches a reference color

Download Script

last modified: December 15, 2018



USAGE: autolabel -s size -t text [-m mode] [-b buffer] [-p pointsize] [-f font] [-r rcolor] [-c color] [-u undercolor] infile outfile
USAGE: autolabel [-h or -help]

-s .... size ............ size textbox; can be specified as WIDTH, WIDTHx,
......................... xHEIGHT or WIDTHxHEIGHT
-t .... text ............ text to apply to image; enclose in quotes
-m .... mode ............ text mode; mode is either label or caption;
......................... default=label
-b .... buffer .......... buffer or padding around text box; integer>=0;
......................... default=0
-p .... pointsize ....... text pointsize; integer>0; default will be
......................... determined from textbox width
-f .... font ............ font name or path to font file; default=Helvetica
-r .... rcolor .......... reference color for determining best textbox
......................... location; default=white
-c .... color ........... text color; any valid IM color specification;
......................... default will be either black or white, whichever
......................... contrasts better with the reference color
-u .... undercolor ...... undercolor for text; any valid IM color
......................... specification; default=none for transparent so that
......................... image coloration shows behind text

PURPOSE: To place text automatically in a specified size region that best matches a reference color.

DESCRIPTION: AUTOLABEL places text automatically in the first located user specified size region that best matches a reference color. The text can be placed in label or caption mode. In label mode, the text will all be on one line determined either by the textbox width or height or by the specified pointsize. In caption mode, the text will be best fit to the textbox width and height and optionally pointsize. By default the text will be placed on the image with no undercolor. But an undercolor can be used which will cover the underlying image.

ARGUMENTS:

-s size ... SIZE of textbox. Also used to find the location in the image that best matches the reference color. In label mode, the text will be placed all in one line. The size of the text will be determined either by the textbox width or height or pointsize. Specifying pointsize and width or height is not a valid combination. In caption mode, the text will be placed in multiple rows as determined by the textbox width and height. Pointsize may optionally be supplied in addition to the textbox width and height.

-t text ... TEXT to apply to image. Be sure to enclose in quotes.

-m mode ... MODE for adding text. Options are either label or caption. In label mode the text will be all in one row as determined by either the textbox width or height or pointsize. In caption mode, the text will be on multiple rows to fill the textbox width and height. The pointsize may be optionally specified along with the textbox width and height.

-b buffer ... BUFFER is the amount of padding around the textbox. Values are integers greater than zero. The default=0.

-p pointsize ... POINTSIZE is the size of the font. Values are integers greater than 0. The default is unspecified, so that the pointsize will be determined from the textbox width, height or widthxheight.

-f font ... FONT is the text font or path to the font file. The default is Helvetica.

-r rcolor ... RCOLOR is the reference color to use to locate a textbox sized region in the image which is closest in color for placing the text. Any valid IM color specification is allowed. The default=white. See http://imagemagick.org/script/color.php

-c color ... COLOR is the text color. Any valid IM color specification is allowed. The default will be either black or white, whichever contrasts best with the reference color. See http://imagemagick.org/script/color.php

-u undercolor ... UNDERCOLOR is the color to use under the text within the textbox. Any valid IM color specification is allowed. The default=none, which means that the text will be placed over the image without any undercolor. If an undercolor is specified, then it will cover the underlying image. See http://imagemagick.org/script/color.php

CAVEAT: No guarantee that this script will work on all platforms, nor that trapping of inconsistent parameters is complete and foolproof. Use At Your Own Risk.


EXAMPLES


Original Image

 

Arguments:
-m label -s 100x -r white -b 5 -t "This Is A Test"

 

Arguments:
-m label -s 100x -r white -u lightblue -b 5 -t "This Is A Test"

 

Arguments:
-m label -p 18 -r white -b 5 -t "This Is A Test"

 

Arguments:
-m caption -s 50x50 -r white -b 5 -t "This Is A Test"

 

Arguments:
-m caption -s 80x80 -r white -b 5 -t "This Is A Test"


What the script does is as follows:

  • Uses either label or caption to create the desired text and
    determines the width and height of the textbox from that image
  • Reduces the textbox size by 1/10 and uses that size filled
    with the reference color to find the best match location in a
    1/10 sized input image using the compare function
  • Scales the match location by 10 for the full scale image
  • Crops out a subsection of the full sized image that is 1.4 times
    the textbox size around that location
  • Then recomputes the match location using the compare function with that
    subsection and the full sized textbox filled with the reference color
  • Finally composites the text filled image over the original image
    at the newly computed match point relative to the full image