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.

TSHIRTWARPER


Transforms an image to place it in a region of a tshirt image

Download Script

last modified: July 17, 2020



USAGE: tshirtwarper [-r region] [-c coords] [-f fit] [-g gravity] [-v vshift] [-o offset] [-R rotate] [-s sharpen] [-d displace] [-a antialias] [-B brightness] [-S saturation] [-C compose] [-O opacity] [-D directory] infile bgfile [maskfile] outfile

or

USAGE: tshirtwarper [-r region] [-c coords] [-f fit] [-g gravity] [-v vshift] [-o offset] [-R rotate] [-s sharpen] [-d displace] [-a antialias] [-B brightness] [-S saturation] [-C compose] [-O opacity] lightingfile displacementfile infile bgfile [maskfile] outfile

USAGE: tshirt [-help|-h]

-r ... region ...... region is a simple rectangle defined as the WxH+X+Y
.................... of the region in the tshirt (bgfile) where the overlay
.................... image (bgfile) will be placed. The components of W, H
.................... X and Y are integers and specify the width, height
.................... xoffset and yoffset of the rectangle. Only one or the
.................... other of region or coordinates should be specified.
.................... But coordinates take precedent.
-c ... coords ...... coords are the 4 x,y corners of the region in the
.................... tshirt (bgfile) where the overlay image (infile) will
.................... be placed; coordinates must be listed clockwise from
.................... top left corner; coordinates are integers>=0. Only
.................... one or the other of region or coordinates should be
.................... specified. But coordinates take precedent. Coordinates
.................... are not restricted to a rectangle and the region
.................... defined by the coordinates can have rotation.
-f ... fit ......... fit can be: crop (c), distort (d) scale (s) or none (n);
.................... If crop, then the overlay image (infile) will be resized to the
.................... width of the region (or coordinate area) and cropped if needed.
.................... This will not distort the image. The image will not be cropped,
.................... if its height is smaller than that of the region and it
.................... will then be aligned at the top of the region. If distort,
.................... then the overlay image (infile) will be resized to fit exactly
.................... to the region (or coordinate area). This will cause distortion,
.................... if the aspect ratio of the overlay image (infile) does not
.................... match the aspect ratio of the region (or coordinate area). If
.................... scale, then the overlay image (infile) will be resized so that
.................... the larger dimension fits the region (or coordinate area) and
.................... the other dimension is smaller. The resized overlay image
.................... (infile) will be centered in the region. The default=none,
.................... which means that the image will be resized to fit the width of
.................... the region (or coordinate area) and aligned at the top of the
.................... region. If the height of the resized image is larger than the
.................... height of the region, it will extend past the bottom of the
.................... region.
-g ... gravity ..... gravity for selecting the crop location; choices are:
.................... north (n), south (s) or center (c). The default=center
-v ... vshift ...... vertical shift of the crop region with respect to the
.................... gravity setting; integer; negative is upward and positive
.................... is downward; default=0 (no shift)
-o ... offset ...... additional x,y offset of the region or coordinates
.................... in order to make positional adjustments easier;
.................... comma separated x,y pair; positive or negative integer;
.................... default is no offset.
-R ... rotate ...... additional clockwise positive rotation in order to make
.................... orientational adjustments easier; -360<=float<=360;
.................... default=0
-s ... sharpen ..... sharpening to apply to the overlay (infile) image;
.................... float>=0; default=1
-d ... displace .... amount of displacement for the distortion of the
.................... overlay (infile) image; integer>=0; default=10
-a ... antialias ... antialias amount to apply to alpha channel of tshirt
.................... (bgfile) image; float>=0; default=2
-B ... brightness .. brightness percent change to the input overlay image;
.................... positive or negative integer; default=0
-S ... saturation .. saturation percent change to the input overlay image;
.................... positive or negative integer; default=0
-C ... compose ..... compose method to merge the input overlay (infile) image with
.................... the background (bgfile); choices are: hardlight, multiply or
................... over; default=hardlight
-O ... opacity ..... opacity to apply to the image; 0<=integer<=100; default=100
................... (fully opaque)
-D ... directory ... directory to read all the exported data from the tshirt script.
.................... This includes the lighting and displacement images and a text file
.................... called tshirtdata.txt containing the same textual parameter data.

infile is the overlay image to apply to the tshirt, which is the bgfile.
maskfile is a mask of part or all of the tshirt to limit the region onto which the
overlay image is to be applied.

PURPOSE: Transforms an image to place it in a region of a tshirt image.

DESCRIPTION: TSHIRTWARPER transforms an image to place it in a region of a tshirt image. The transformed image will display hightlights from the tshirt image and be distorted to match the wrinkles in the tshirt image. The tshirt script must be run once first using -E export to get at least the lighting and displacement images. This can be done with or without -d directory.

The main purpose of this script and difference from the tshirt or tshirtwarp script is to allow the overlay image to be moved as desired, but avoid having to recreating the lighting and displacement images.

If the -D directory is provided, default arguments will be taken from that directory's tshirtdata.txt as well as the lighting and displacement images. This directory needs to have been created previously by running the tshirt script with the -D and -E arguments. The arguments to the script can be used to override ones from the data in the directory's tshirtdata.txt file. However, either region or coords must be supplied, since the tshirtdata.txt file's coordinates will be ignored. Use tshirtwarp rather than tshirtwarper, if you want to reuse the coordinates and other data in the tshirtdata.txt file.

If -D is not supplied, then the script will expect that the lighting and displacement images will be supplied as input images. The script's default arguments will be the same ones in the tshirt script's default arguments, but can be replaced by any arguments provide to this script. However, either region or coords must be supplied.

ARGUMENTS:

-r region ... REGION is a simple (unrotated) rectangle defined as the WxH+X+Y of the region in the tshirt (bgfile) where the overlay image (bgfile) will be placed. The components of W, H, X, Y are integers and specify the width, height, upperleft xoffset and yoffset of the rectangle. Only one or the other of region or coordinates should be specified. But coordinates take precedent.

-c coords ... COORDS are the 4 x,y corners of the region in the tshirt (bgfile) where the overlay image (bgfile) will be placed. The coordinates must be listed clockwise from top left corner; coordinates are integers>=0. Only one or the other of region or coordinates should be specified. But coordinates take precedent. Coordinates are not restricted to a rectangle and the region defined by the coordinates can have rotation.

-f fit ... FIT can be: crop (c), distort (d) scale (s) or none (n); If crop, then the overlay image (infile) will be resized to the width of the region (or coordinate area) and cropped if needed. This will not distort the image. The image will not be cropped, if its height is smaller than that of the region and it will then be aligned at the top of the region. If distort, then the overlay image (infile) will be resized to fit exactly to the region (or coordinate area). This will cause distortion, if the aspect ratio of the overlay image (infile) does not match the aspect ratio of the region (or coordinate area). If scale, then the overlay image (infile) will be resized so that the larger dimension fits the region (or coordinate area) and the other dimension is smaller. The resized overlay image (infile) will be centered in the region. The default=none, which means that the image will be resized to fit the width of the region (or coordinate area) and aligned at the top of the region. If the height of the resized image is larger than the height of the region, it will extend past the bottom of the region.

-g gravity ... GRAVITY for selecting the crop location. The choices are: north (n), south (s) or center (c). The default=center.

-v vshift ... VSHIFT is the vertical shift of the crop region with respect to the gravity setting. Values are integer. Negative is upward and positive is downward. The default=0 (no shift).

-o offset ... OFFSET is an additional x,y offset of the region or coordinates in order to make positional adjustments easier. This is a comma separated x,y pair. Values are ositive or negative integers for x and y. The default is no offset.

-R rotate ... ROTATE is an additional clockwise positive rotation in order to make orientational adjustments easier. Values are -360<=float<=360. The default=0.

-l lighting ... LIGHTING is the contrast increase/decrease for highlights to apply to the overlay image (infile). Values are -100<=integer<=100. Positive values increase contrast and negative values decrease contrast. The default=20.

-s sharpen ... SHARPEN is the sharpening to apply to the overlay (infile) image. Values are floats>=0. The default=1.

-d displace ... DISPLACE is the amount of displacement for the distortion of the overlay (infile) image. Values are integers>=0. The default=10.

-a antialias ... ANTIALIAS amount to apply to alpha channel of tshirt (bgfile) image. Values are floats>=0. The default=2.

-A attenuate ... ATTENUATE is the attenuation of the effect of coarse texture from the background (bgfile) image from showing on the overlay (input) image. Values are floats>=0. Larger values blur the tshirt texture more which attenuates the effects. The default=0.

-B brightness ... BRIGHTNESS percent change to the input overlay image. Value are positive or negative integers. The default=0.

-S saturation ... SATURATION percent change to the input overlay image. Values are positive or negative integers. The default=0.

-C compose ... COMPOSE method to merge the overlay (infile) image with the background (bgfile). Choices are: hardlight (h), multiply (m) or over (o). The default=hardlight. The choice of hardlight allows the overlay image to be brighten and darken according to the folds in the tshirt. The other compose methods will not do that. The choice of over simply places the overlay image unchanged upon the tshirt image. The choice of multiply does a multiply operation, which may change colors and would need adjustment by -B and -S arguments.

-O opacity ... OPACITY to apply to the image. Values are 0<=integers<=100. The default=100 (fully opaque).

-D directory ... DIRECTORY to write all the exported data when using the -E export option, including a text file called tshirtdata.txt that contains the same textual parameter data sent to terminal. The directory can be specified in the tshirtwarp script to import all the needed images and textual data. This permits one tshirtwarp script to be used with all style tshirts, rather than save a customized script for each style tshirt. If the directory does not exist, it will be created.

IMPORTANT: The nominal transformed image will not fit the exact height of the region or coordinates area, if the aspect ratio of the image differs from that of the region or coordinate area. The process maintains the correct aspect ratio of the input image. Depending upon the aspect ratio, the transformed image may be shorter or taller than the height of the region. To crop the image so that it fits the region or coordinate area, use the -f option.

REQUIREMENTS: IM 6.5.3.4 due to the use of -compose displace.

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


First Run Tshirt Script With Option -E To Get Lighting Image, Displacement Map and Terminal Listing

Input (Overlay) Image
(Source Image)

Background (Tshirt )Image
(Source Image)

Arguments:
-r "130x130+275+175" -R -3 -o 5,0 -E

Lighting Image

Displacement Image

Terminal Listing

coordinates="0,0 276,179 478,0 405,172 478,478 411,300 0,478 283,307"
sharpen="1"
displace="10"
modulating=""
compose="hardlight"



Now run the tshirtwarper script
( speed gain about 2x )

tshirtwarper -r "130x130+275+175" -R -3 -o 5,0 lighting.png displace.png overlayfile tshirtfile outputfile

(provide paths as needed)

Lighting Image

Displacement Image

Input (Overlay) Image
(Source Image)

Background (Tshirt )Image
(Source Image)

Arguments:
(none needed)



Alternately, Run Tshirt Script With Option -E -D path2/data_directory
(where you specify some data_directory to put all the needed files)

Input (Overlay) Image
(Source Image)

Background (Tshirt )Image
(Source Image)

Arguments:
-r "130x130+275+175" -R -3 -o 5,0 -E -D path2/data_directory

Textual Data listed At the terminal and written to a file called tshirtdata.txt in the data_directory

coordinates="0,0 276,179 478,0 405,172 478,478 411,300 0,478 283,307"
antialias="2"
sharpen="1"
displace="10"
modulating=""
compose="hardlight"



Now Run The tshirtwarper script supplying data_directory followed by your overlayfile, tshirtfile and outputfile
( speed gain about 2x )

tshirtwarper -D path2/data_directory overlayfile tshirtfile outputfile

(provide paths as needed)

Input (Overlay) Image
(Source Image)

Background (Tshirt )Image
(Source Image)

Arguments:
-D path2/data_directory



What the script does is as follows:

  • Requires that Tshirt Script be run first with options -E -D path2/data_directory
    to export the lighting and displacement map image and arguments
    listed to the terminal
  • Run Tshirtwarper with -D path2/data_directory,
    input image, background image and output image.