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.

SPHERIZE


Warps an image onto a (hemi-)sphere.

Download Script

last modified: December 15, 2018



USAGE: spherize [-d diameter(s)] [-a amp] [-z zoom] [-b bcolor ] [-s] [-t] infile outfile
USAGE: spherize [-h or -help]

-d ...... diameter(s) ....... diameters; comma separate x and y diameters;
............................. integers>0 and less than or equal to image
............................. dimensions; if only one value provided,
............................. it will be used for both; default is the
............................. image "width,height"
-a ...... amp ............... distortion amplification factor; float>=0;
............................. default=0
-z ...... zoom .............. zoom factor; scales the input image on the
............................. sphere; float>=0; default=1
-b ...... bcolor ............ background color; any valid IM color or
............................. "none" for transparent or "image" for input
............................. image background; default=black
-s .......................... make result with both diameters equal to the
............................. smaller of the x or y diameters
-t .......................... trim image to diameters

PURPOSE: To warp an image onto a (hemi-)sphere.

DESCRIPTION: SPHERIZE warps an image onto a (hemi-)sphere and views it orthographically. The x and y diameters can be specified along with the distortion amplification and input image zoom. The background around the sphere is also user controlled. This is a limited version of my bubblewarp script that is not limited by the use of the IM -fx function and therefore is very fast.

ARGUMENTS:

-d diameter(s) --- DIAMETER(S) is a comma separate list of the desired x and y diameters of the sphere. If only one value is provided, then it will be used for both diameters. Values are integers greater than zero and equal to or smaller than the dimensions of the image. The default values are the size of the image. Thus if the image is square, then a spherical effect will be produced. But if the image is not square, then elliptical effect will be produced.

-a amp --- AMP is the distortion amplification factor. Values are floats greater than or equal to zero. The default=1. Values larger than one will make more distortion and values less than one will make less distortion.

-z zoom --- ZOOM is the zoom factor for the input image before warping it onto the sphere. Values are floats greater than or equal to zero. The default=1. Values larger than one will magnify the input image on the sphere. Values less than one will minify the input image on the sphere.

-b bcolor ... BCOLOR is the color for the background outside the sphere. Any valid IM color is allowed or one may specify "none" for a transparent background or "image" to have the input image as the background. The default is black.

-s ... Make the result with both diameters equal to the smaller of the x or y diameters.

-t ... Trim the output image to the diameters.

NOTE: This script is a limited version of my bubblewarp script, but is much faster, since it does not use -fx.

NOTE: This script requires IM 6.5.3-9 or higher due to the use of -compose distort. Many thanks to Anthony Thyssen for his hard work developing it.

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


Original

Arguments:
-b black
(default)

Arguments:
-b none

Arguments:
-b image

Arguments:
-a 1
(default)

Arguments:
-a 1.2

Arguments:
-a 0.8

Arguments:
-z 1
(default)

Arguments:
-z 1.2

Arguments:
-z 0.8

Arguments:
-d "96,96"

Arguments:
-d "96,96" -t

Arguments:
-d "96,96" -a 1.2 -t



Original

Arguments:
-b black

Arguments:
-b black -s



What the script does is as follows:

  • Creates a radial gradient (R)
  • Thresholds the radial gradient for a mask for later use
  • Transforms the radial gradient intensity into arcsin(R)/(R)
  • Creates a horizontal gradient (X)
  • Applies a special product of the linear gradient (X) and the
    arcsin(R)/(R) to form the horizontal displacement map
  • If the diameters are equal, rotates the horizontal displacement
    map by 90 degrees to form the vertical displacement map;
    otherwise repeats the special product with a vertical gradient (Y)
  • Applies the horizontal and vertical displacement maps to the image
    with -compose distort
  • Masks the result

This is equivalent to the following IM commands for the
case of the default spherize effect on a square image.

  • width=`identify -ping -format %w $infile`
  • height=`identify -ping -format %h $infile`
  • xamount=`convert xc: -format "%[fx:$ww/(2*$zoom)]" info:`
  • yamount=`convert xc: -format "%[fx:$hh/(2*$zoom)]" info:`
  • convert -size ${dm}x${dm} radial-gradient: -negate $tmpF1
  • convert $tmpF1 -negate -threshold 0 $tmpM1
  • convert $tmpF1 \( +clone -function arcsin '2,0,2,0' \) \
    -compose divide -composite $tmpF1
  • convert -size ${dy}x${dx} gradient: -rotate 90 $tmpX1
  • convert $tmpX1 $tmpF1 \ \( -clone 0 -solarize 50% -level 50,0% \) \
    \( -clone 1,2 -compose multiply -composite -function polynomial "0.5,0.5" \) \
    \( -clone 0 -threshold 50% \) \
    \( -clone 3,4 -compose multiply -composite \) \
    \( -clone 3,4 -negate -compose multiply -composite \) \
    -delete 0-4 -compose plus -composite \
    $tmpX1
  • convert $tmpX1 -rotate 90 $tmpY1
  • convert $tmpA1 $tmpX1 $tmpY1 \
    -gravity center -compose distort \
    -set option:compose:args ${xamount}x${yamount} -composite \
    $tmpA1
  • convert \( $tmpA1 $tmpM1 \
    -gravity center -compose copy_opacity -composite \) \
    \( -size ${ww}x${hh} xc:"$bcolor" \) +swap \
    -gravity center -compose over -composite $outfile