Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial 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

TEXTDISTORT


Distorts repeated text to warp it to the shape of the image content.

Download Script

last modified: September 26, 2017



USAGE: textdistort -t text [-p pointsize] [-f font] [-c color] [-s soften] [-C contrast] [-d distort] [-r roll] [-b background] [-u ucolor] [-P] [-R rfactor] infile outfile
USAGE: textdistort [-help or -h]

-t ... text ........ text to apply to the image. Required.
-p ... pointsize .... pointsize for the text; integer>0; default=16
-f ... font ......... font name or path to font file; default=arial
-c ... color ........ color of font; any valid opaque IM color is allowed;
..................... default=white
-s ... soften ....... soften (blur) the grayscale version of the image used in the
..................... distort processing; integer>=0; default=20
-C ... contast ...... sigmoidal-contrast adjustment of the grayscale image to increase
..................... contrast to make the image stand out when using
..................... background=undercolor and also to help distort the text;
..................... integer>=0; default=5
-d ... distort ...... comma separate pair of distortion values for x and y distortion
..................... directions; integers>=0; default="10,10"
-r ... roll ......... roll the text overlay to the left to adjust for distortion bias;
..................... integer>=0; default=0
-b ... background ... background type; choices are: undercolor (u) to use a constant
..................... color under the text or image (i) to use the original image
..................... under the text; default=undercolor
-u ... ucolor ....... undercolor color; any valid opaque IM color is allowed;
..................... default=black
-P .................. use pango: rather than caption: to create the text; pango will
..................... justify the text; caption will center the text as it wraps;
..................... pango must be installed as a delegate to use it; default is
..................... to use caption:
-R ... rfactor ...... rfactor is a repeat factor to adjust the automatic computation
..................... of how many times to repeat the text; it is needed when fonts
..................... are not monospaced; float>=1; default=2.5

.

PURPOSE: Distorts repeated text to warp it to the shape of the image content.

DESCRIPTION: TEXTDISTORT distorts repeated text to warp it to the shape of the image content. The image is typically a human face. The background of the result may be some solid color or the original image.

ARGUMENTS:

-t text ... TEXT to apply to the image. Required.

-p pointsize ... POINTSIZE for the text. Values are integer>0. The default=16.

-f font ... FONT name or path to font file. The default=arial.

-c color ... COLOR of the font. Any valid opaque IM color is allowed. The default=white.

-s soften ... SOFTEN (blur) the grayscale version of the image used in the distort processing. Values are integers>=0. The default=20.

-C contast ... CONTRAST is a sigmoidal-contrast adjustment of the grayscale image to increase contrast in order to make the image stand out when using background=undercolor and also to help distort the text better. Values are integers>=0. The default=5.

-d distort ... DISTORT is a comma separate pair of distortion values for the x and y distortion directions. Values are integers>=0. The default="10,10".

-r roll ... ROLL the text overlay to the left to adjust for distortion bias. Values are integers>=0. The default=0.

-b background ... BACKGROUND is the type of background for the text. The choices are: undercolor (u) to use a constant color under the text or image (i) to use the original image under the text. The default=undercolor.

-u ucolor ... UCOLOR is the color to use for the background. Any valid opaque IM color is allowed. The default=black.

-P ... USE PANGO: rather than CAPTION: to create the text overlay. Pango will justify the text, while caption will center the text as it wraps. Pango will run faster than caption and may require smaller pointsizes to match that of caption. Pango must be installed as a delegate to use it. The default is to use caption:

-R rfactor ... RFACTOR is a repeat factor to adjust the automatic computation of how many times to repeat the text. It is needed when fonts are not monospaced. Values are floats>=1. The default=2.5

Reference:
https://blog.spoongraphics.co.uk/tutorials/how-to-create-a-text-portrait-effect-in-photoshop

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


Example 1 -- Background = undercolor

Original Image

 

Arguments:
-t "Now is the time for all good men to come to the aid of their country." -p 16 -r 5

 

Arguments:
-t "Now is the time for all good men to come to the aid of their country." -p 13 -r 0 -P


Example 2 -- Background = image

Original Image

 

Arguments:
-t "Now is the time for all good men to come to the aid of their country." -p 16 -c black -r 5

 

Arguments:
-t "Now is the time for all good men to come to the aid of their country." -p 13 -c black -r 0 -P


What the script does is as follows:

  • Read the input image
  • Repeat the text as many times as needed to more than fill the image
  • Create a text image using the repeated text string on a transparent
    background to fill the image dimensions
  • Optionally composite the text image onto a solid color background or onto the original image
  • Write the output image

  • convert -quiet -regard-warnings "$infile" +repage "$tmpA1"
  • declare `convert -ping $infile -format "ww=%w\nhh=%h" info:`
  • tot=$((ww*hh))
  • distortx=`echo "$distort" | cut -d, -f1`
  • distorty=`echo "$distort" | cut -d, -f2`
  • count=`echo "$text" | wc -c`
  • repeats=`convert xc: -format "%[fx:round($rfactor*$tot/($pointsize*$pointsize*$count))]" info:`
  • fulltext=""
  • for ((i=0; i fulltext="$fulltext $text"
    done
  • if [ "$pango" = "yes" ]; then
    # use pango:
    convert -size ${ww}x -background none -pointsize $pointsize \
    -font $font -fill $color -define pango:justify=true pango:"$fulltext" \
    -gravity north -crop ${ww}x${hh}+0+0 +repage $tmpA2
    else
    # use caption:
    convert -size ${ww}x -background none -pointsize $pointsize \
    -font $font -fill $color -gravity center caption:"$fulltext" \
    -gravity south -background black -splice 0x1 -trim +repage -chop 0x1 \
    -gravity north -crop ${ww}x${hh}+0+0 +repage $tmpA2
    fi
  • if [ "$background" = "undercolor" ]; then
    convert $tmpA2 \
    \( $tmpA1 -colorspace gray -sigmoidal-contrast ${contrast}x50% -write mpr:mask -blur 0x$soften \) \
    -virtual-pixel none -define compose:args=-$distortx,-$distorty -compose displace -composite \
    \( +clone -alpha extract \( mpr:mask \) -compose over -compose multiply -composite -auto-level \) \
    -alpha off -compose copy_opacity -composite -roll -${roll}+0 \
    -compose over -background $ucolor -flatten \
    "$outfile"
    elif [ "$background" = "image" ]; then
    convert $tmpA1 \
    \( $tmpA2 \
    \( $tmpA1 -colorspace gray -sigmoidal-contrast ${contrast}x50% -blur 0x$soften \) \
    -virtual-pixel none -define compose:args=-$distortx,-$distorty -compose displace -composite -roll -${roll}+0 \) \
    -gravity center -compose over -composite \
    "$outfile"
    fi