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.

AUTOCAPTION


Places text automatically in a specified square size region that has the least color variation.

Download Script

last modified: December 15, 2018



USAGE: autocaption -s size -t text [-b buffer] [-f font] [-c color] [-u undercolor] [-o outlinecolor] [-w width] infile outfile
USAGE: autocaption [-h or -help]

-s .... size ............ size of square textbox; integer>0
-t .... text ............ text to apply to image; enclose in quotes
-b .... buffer .......... buffer or padding around text box; integer>=0;
......................... default=0
-f .... font ............ font name or path to font file; default=Helvetica
-c .... color ........... text color; any valid IM color specification;
......................... contrasts best with the color of the region that
......................... was found by the search
-u .... undercolor ...... undercolor for text; any valid IM color
......................... specification; default=none for transparent so that
......................... image coloration shows behind text
-o ...................... outlinecolor for text; any valid IM color
......................... specification; default=none for transparent so that
......................... there is effectively no outline; if this is not
......................... none, then no contrast will be used and the text
......................... color will default to white.
-w ....width ............ width (thickness) of outline; integer>=0; default=1

PURPOSE: To place text automatically in a specified square size region that has the least color variation throughout the image.

DESCRIPTION: AUTOCAPTION places text automatically in a specified square size region that has the least color variation throughout the image. The text will be placed at the first acceptable location found. 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 square textbox. Also used to find the location in the image that has the least color variation. The text will be placed in multiple rows as determined by the textbox size.

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

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

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

-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 color of the region that was found by the search. 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

-o outlinecolor ... OUTLINECOLOR is the color to use for outline style text. Any valid IM color specification is allowed. The default=none for transparent so that there is effectively no outline. if this is not none (recommend black), then no contrast will be used and the text color will default to white.

-w width ... WIDTH (thickness) of outline. Values are integers>=0. The default=1

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:
-s 100 -t "this is a test"

 

Arguments:
-s 100 -t "this is a test" -c blue -u pink


What the script does is as follows:

  • Gets the radius for -blur from the padded window size
  • Computes the local channel standard deviation within the window size about each pixel using -blur
  • Averages the channel results and negates so the smallest standard deviation is whitest
  • Find that pixel in the resulting image with the largest value (most homogenous)
  • Writes the text into the given size window
  • Places the window over the image at the given location found