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

TSHIRT


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

Download Script

last modified: April 27, 2016



USAGE: tshirt [-r region] [-c coords] [-f] [-g gravity] [-v vshift] [-o offset] [-R rotate] [-l lighting] [-s sharpen] [-b blur] [-d displace] [-a antialias] [-E] infile bgfile 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 (bgfile) 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 is either crop (c) or distort (d); If crop, then
.................... the overlay image (infile) will be cropped to make
.................... its vertical aspect ratio fit that of the region
.................... or coordinate area. This will not distort the image,
.................... only make it fit the size of the region or coordinate
.................... area. The image will not be cropped, if its vertical
.................... aspect ratio is smaller than that of the region. If
.................... distort, the overlay image (infile) will be fit to the
.................... region or coordinate area, if the aspect ratio of the
.................... overlay image (infile) does not match that of the region
.................... or coordinate area. The default=crop.
-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
-l ... lighting .... lighting is the contrast increase for highlights to
.................... apply to the overlay image (infile); 0<=integer=<30;
.................... default=20
-s ... sharpen ..... sharpening to apply to the overlay (infile) image;
.................... float>=0; default=1
-b ... blur ........ blurring to apply to the displacement map to avoid
.................... jagged displacement; 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
-E ... export ...... export the lighting image, displacement map and other
.................... needed parameters to be able to use a condensed script
.................... to repeat the processing on the same size input images
.................... for faster processing.
infile is the overlay image to apply to the tshirt, which is the bgfile

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

DESCRIPTION: TSHIRT 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.

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 is crop (c), distort (d) or none. If crop, then the overlay image (infile) will be cropped to make its vertical aspect ratio fit that of the region or coordinate area. This will not distort the image, only make it fit the size of the region or coordinate area. The image will not be cropped, if its vertical aspect ratio is smaller than that of the region. If distort, the overlay image (infile) will be fit to the region or coordinate area, if the aspect ratio of the overlay image (infile) does not match that of the region or coordinate area. The default is none.

-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 for highlights to apply to the overlay image (infile). Values are 0<=integer<=30. The default=20.

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

-b blur ... BLUR is the blurring to apply to the displacement map to avoid jagged displacement. 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.

-E ... EXPORT the lighting image, displacement map and other needed parameters for use with a condensed script to repeat the processing on the same tshirt image apart from coloring for faster processing. The lighting image will be named lighting.png and the displacement map will named displace.png. The other arguments will be displayed to the terminal and can be copied and pasted into other scripts. See my script, tshirtwarp.

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


Example 1 -- Gray Tshirt and Opaque Image

Input (Overlay) Image
(Source Image)

Background (Tshirt )Image
(Source Image)

Background (Tshirt )Image With Selected Bounding Box

Arguments:
-r "130x130+275+175"
or
-c "275,175 404,175 404,304 275,304"

Arguments:
-r "130x130+275+175" -f crop -g north

Arguments:
-r "130x130+275+175" -f crop -g center

Arguments:
-r "130x130+275+175" -f distort

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



Example 2 -- Blue Tshirt and Opaque Image

Background (Tshirt )Image
(Source Image)

Arguments:
-r "130x130+275+175"



Example 3 -- Gray Tshirt and Transparent Image

Input (Overlay) Image
(Source Image)

Background (Tshirt )Image
(Source Image)

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



What the script does is as follows:

  • Converts the image to grayscale
  • Gets the region and computes the grayscale mean and difference from mid gray
  • Adjust the grayscale image so that the region is mid gray
  • Modifies the mid gray image by adjusting the contrast to create the lighting image
  • Blurs the mid gray image to create the displacement image
  • Sharpens and crops the overlay image
  • Uses the bounding coordinates to perspectively adjust the overlay image to the desired size
    and inserts that into a transparent background image the size of the tshirt image
  • Applies the lighting image to the modified overlay image
  • Applies the displacement image to the lighting adjusted modified overlay image
  • Composites the final overlay image onto the tshirt image