Fred's ImageMagick Scripts


    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:

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


Warps an image according to a user supplied triangular mesh.

Download Script

last modified: December 09, 2023

USAGE: meshwarp -f file [-i] [-p] infile outfile
USAGE: meshwarp [-h or -help]

-f ... file ....... textfile containing src and dst x,y control point pairs
................... and triangle vertex indices
-i ................ display information about number of src, dst and triangles
-p ................ display triangle progress

PURPOSE: To warp an image according to a triangular mesh.

DESCRIPTION: MESHWARP warps an image according to a user supplied triangular mesh. The mesh list src and dst x,y control points per line. Then a list of triangle vertex indices is listed one triangle per line. This script may be used to simply warp part or all of an image or to warp one image to match another.


-f file ... FILE is a text file containing first a pair of scr and dst x,y control points separated by a space. The following that is a list of 3 comma separate triangle vertex indices. Vertex indices start at 0 and reference the corresponding pair of src and dst x,y control points.

-i ... Enable the listing to the terminal of number of src, dst and triangles extracted from the file.

-p ... Enable the listing to the terminal of the triangle numbers as the triangles are processed.

Note: the script may be rather slow due to the processing of each triangle one-at-a-time.

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.


Textfile With Control Points and Triangle Vertex Indices

Original Image


-f meshwarp_data.txt


Input vs Output Animation


Input Image With Blue SRC Trianglulation


Input Image With Blue SRC And Red DST (and Purple Overlapping) Trianglulation


Output Image With Red DST Trianglulation

What the script does is as follows:

  • Create an initial transparent image the size of the input as a
    background image
  • Apply an affine warp of the input image for the bounding box
    about a given dst triangle
  • Extend with transparency the warped bounding box image data
    and translate to the proper offset
  • Repeat for next dst triangle

This is equivalent to the following IM commands.

  • convert -quiet -regard-warnings "$infile" -depth 8 $dir/tmpI.mpc
  • ww=`identify -ping -format "%w" $dir/tmpI.mpc`
  • hh=`identify -ping -format "%h" $dir/tmpI.mpc`
  • convert -size "${ww}x${hh}" xc:none $dir/tmpT.mpc
  • for ((i=0; i<numt; i++)); do
    getTriangle "${triArr[$i]}"
    convert $dir/tmpT.mpc \
    \( $dir/tmpI.mpc -define distort:viewport=${wd}x${ht}+${xmin}+${ymin} \
    -distort Affine "$s1x,$s1y $d1x,$d1y $s2x,$s2y $d2x,$d2y $s3x,$s3y $d3x,$d3y" \
    -background none -extent ${ww}x${hh} -roll +${xmin}+${ymin} \) \
    \( -size "${ww}x${hh}" xc:black +antialias -fill white -draw "polygon $d1 $d2 $d3" \) \
    -compose over -composite $dir/tmpT.mpc done
  • convert $dir/tmpT.mpc $outfile