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: September 26, 2017



USAGE: tshirt [-r region] [-c coords] [-f fit] [-g gravity] [-v vshift] [-o offset] [-R rotate] [-l lighting] [-s sharpen] [-b blur] [-d displace] [-a antialias] [-A attenuate] [-B brightness] [-S saturation] [-C compose] [-E] [-D directory] 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/decrease for highlights to
.................... apply to the overlay image (infile); -100<=integer=<100;
.................... 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
-A ... attenuate ... attenuation of the effect of coarse texture on the background
.................... (bgfile) image from showing on the overlay (input) image;
.................... float>=0; larger values blur the tshirt texture more which
.................... attenuates the effects; default=0
-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
-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 and style tshirt image
.................... for faster processing.
-D ... directory ... directory to write all the exported data, including a text file
.................... called tshirtdata.txt containing 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.

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/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.

-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.

-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.

-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 my script, tshirtwarp.

-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


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



Example 4 -- Textured Gray Tshirt and Opaque Image

Input (Overlay) Image
(Source Image)

Background (Tshirt )Image
(Source Image)

Arguments:
-r "190x280+210+160"

Arguments:
-r "190x280+210+160" -b 3

Arguments:
-r "190x280+210+160" -b 3 -l -10

Arguments:
-r "190x280+210+160" -b 3 -l -10 -A 5



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