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

PAGEPEEL


Applies a pagepeel effect to the lower right corner of an image.

Download Script

last modified: September 26, 2017



USAGE: pagepeel [-a amount] [-p pcolor] [-b bgcolor] [-s shortening] [-c curvature] [-m mode] [-S shadow] [-D darklevel] [-B brighten] [-C contrast] [-I] infile [bgfile] outfile
USAGE: pagepeel [-help]

-a .... amount .......... amount of pagepeel expressed as percent of
......................... image diagonal; integer>0; default=30
-p .... pcolor .......... color to apply to peel; any valid IM color;
......................... default=white
-b .... bgcolor ......... background color to apply to image where
......................... peeled away; any valid IM color;
......................... default=none (transparent)
-s .... shortening ...... percent of peeled over amount relative to
......................... peeled away amount; 0<=integer<=100;
......................... default=90
-c .... curvature ....... proportional to the depth/half-width of the curve;
......................... 0<=integer<=100; default=30
-m .... mode ............ mode of curvature; choices are:
......................... parabola (or p) or arc (or a);
......................... default=parabola
-S .... shadow .......... size of shadow; integer>=0; default=0
-D .... darklevel ....... dark level of shading; 0<=integer<=100;
......................... smaller numbers are darker; default=25
-B .... brighten ........ brightenning percent of light area of
......................... shading; 0<=integer<=100; default=10
-C .... contrast ........ non-linear contrast reduction of shading;
......................... 0<=integer<=100; smaller values reduce
......................... contrast more; default=50
-I ...................... save intermediate mask images;
......................... default=no

PURPOSE: Applies a pagepeel effect to the lower right corner of an image.

DESCRIPTION: PAGEPEEL applies a pagepeel effect to the lower right corner of an image along the bottom right to top left diagonal. The peeled region is outlined by either parabolas or circular arcs. The peeled region can be shaded and/or colored. A shadow can also be added. The removed area can be colored, transparent or filled with an optional same size background image if provided. Note that this is a 2D simulation and not a true 3D effect.

ARGUMENTS:

-a amount ... AMOUNT of pagepeel expressed as percent of image diagonal. Values are in range integer>=1. Recommend at least 5. The default=30

-p pcolor ... PCOLOR is the color to apply to peeled region. Any valid IM color is allowed. The default=white.

-b bgcolor ... BGCOLOR is the color to apply to peeled away part of the image. Any valid IM color is allowed. The default=none for transparent. If a background file is provided, bgcolor must be none.

-s shortening ... SHORTENING is the percent of peeled over amount relative to peeled away amount. Values are integers in the range 0 to 100. The default=90

-c curvature ... CURVATURE is proportional to the ratio of depth/half-width of the curve. Values are integers in the range 0 to 100. The default=30.

-m mode ... MODE of curvature. The choices are: parabola (or p) or arc (or a). The arc is slightly faster. The default=parabola

-S shadow ... SHADOW size. Values are integers>=0. The default=0

-D darklevel ... DARKLEVEL of shading. Values are integers between 0 and 100. Smaller numbers are darker. The default=25

-B brighten ... BRIGHTEN percent of light area of shading. Values are integers between 0 and 100. The default=10

-C contrast ... (non-linear) CONTRAST reduction of shading. Values are integers between 0 and 100. Smaller values reduce contrast more. The default=50

-I ... SAVE intermediate mask IMAGES. The images will be named: pagepeel_corner_mask.png, pagepeel_gradient_mask.png and if shadow != 0, then also pagepeel_shadow_mask.png This allows the same masks to be used for multiple images. See example at http://www.imagemagick.org/Usage/thumbnails/#pagecurl

REQUIREMENTS: IM 6.4.3-0 or higher due to the use of -sparse-color barycentric to generate the gradient shading.

Thanks to Anthony Thyssen for suggesting and working out the shadow and intermediate image save processing.

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


Variation In Amount Of Peel -- Background Transparent

Original Image

Arguments:
-a 10 -b none

Arguments:
-a 30 -b none

Arguments:
-a 50 -b none



Variation In Amount Of Peel -- Background Red -- Peel Gold

Original Image

Arguments:
-a 10 -b red -p gold

Arguments:
-a 30 -b red -p gold

Arguments:
-a 50 -b red -p gold



Variation In Amount Of Peel -- Background Image -- Peel Gold

Original Image

Arguments:
-a 10 -b none -p gold

Arguments:
-a 30 -b none -p gold

Arguments:
-a 50 -b none -p gold



Variation In Amount Of Curvature -- Background Image -- Peel Gold

Original Image

Arguments:
-a 30 -c 10 -b none -p gold

Arguments:
-a 30 -c 30 -b none -p gold

Arguments:
-a 30 -c 50 -b none -p gold



Variation In Shading Parameters -- With Background Image -- Peel Gold

Original Image

Arguments:
-a 50 -b none -p gold -D 10 -B 10 -C 50

Arguments:
-a 50 -b none -p gold -D 10 -B 20 -C 50

Arguments:
-a 50 -b none -p gold -D 10 -B 20 -C 40



Variation In Amount With Different Shading Parameters

Original Image

Arguments:
-a 10 -b none -p gold -D 10 -B 20 -C 40

Arguments:
-a 30 -b none -p gold -D 10 -B 20 -C 40

Arguments:
-a 50 -b none -p gold -D 10 -B 20 -C 40



Variation In Amount With Shadows

Original Image

Arguments:
-a 10 -b none -p gold -S 5

Arguments:
-a 30 -b none -p gold -S 5

Arguments:
-a 50 -b none -p gold -S 5



Animations
(scripts modified from one provided by Anthony Thyssen)

Original Image

Half Peel -- Arc Mode With Shadow

image="mandril3.png"
bgimage="zelda1.jpg"
for ((i=5; i<=55; i=i+5)); do
echo >&2 i=$i
pagepeel -a $i -p gold -S 5 -m arc "$image" "$bgimage" pam:
done | convert \
-dispose background \
-delay 100 "$image" \
-delay 10 - \
\( -delay 10 -clone -1-1 \) \
-delay 100 "$image" \
-loop 0 -layers Optimize \
pagepeel_animation6.gif

Half Peel -- Parabola Mode With Shadow

image="mandril3.png"
bgimage="zelda1.jpg"
for ((i=5; i<=55; i=i+5)); do
echo >&2 i=$i
pagepeel -a $i -p gold -S 5 "$image" "$bgimage" pam:
done | convert \
-dispose background \
-delay 100 "$image" \
-delay 10 - \
\( -delay 10 -clone -1-1 \) \
-delay 100 "$image" \
-loop 0 -layers Optimize \
pagepeel_animation5.gif

Full Peel -- Parabola Mode Without Shadow

image="mandril3.png"
bgimage="zelda1.jpg"
for ((i=5; i<=100; i=i+5)); do
echo >&2 i=$i
pagepeel -a $i -p gold "$image" "$bgimage" pam:
done | convert \
-dispose background \
-delay 100 "$image" \
-delay 10 - \
-delay 100 "$bgimage" \
\( -delay 10 -clone -2-1 \) \
-delay 100 "$image" \
-loop 0 -layers Optimize \
pagepeel_animation4.gif



What the script does is as follows for mode=arc with no shadow:

  • Use parametric equation for straight line from bottom-right to top-left
    corners to find a point along the line
  • Set the distance from the bottom-right corner to the point to be the peeled amount
  • Get a shortened distance on the other side of the point to identify the peel vertex
  • Compute the intersection of the perpendicular line through the point
    with the right side and bottom side of the image
  • Compute the radius of the arc from the half-width defined by the points above
    and the curv parameter
  • Create white and transparent mask for the removed region
  • Composite the mask with image and either background color or background image
  • Create black and transparent mask for the peel region
  • Blur the mask for peel region
  • Composite the blurred mask with previous composite of image and background
  • Create a colored gradient using -sparse-color two-point method
  • Put the non-blurred peel mask into the alpha channel for gradient
  • Composite the masked gradient over the previous composite of image and background

This is equivalent to the following IM commands:

  • ww=`convert $infile -ping -format "%w" info:`
  • hh=`convert $infile -ping -format "%h" info:`
  • wm1=`convert xc: -format "%[fx:$ww-1]" info:`
  • hm1=`convert xc: -format "%[fx:$hh-1]" info:`
  • xf=`convert xc: -format "%[fx:(1-$amount/100)*$wm1]" info:`
  • yf=`convert xc: -format "%[fx:(1-$amount/100)*$hm1]" info:`
  • xr=$wm1
  • yr=`convert xc: -format "%[fx:-($wm1/$hm1)*($xr-$xf)+$yf]" info:`
  • yb=$hm1
  • xb=`convert xc: -format "%[fx:-($hm1/$wm1)*($yb-$yf)+$xf]" info:`
  • test1=`convert xc: -format "%[fx:$yr<0?1:0]" info:`
  • if [ $test1 -eq 1 ]; then
    yt=0
    xt=`convert xc: -format "%[fx:($hm1/$wm1)*$yf+$xf]" info:`
    fi
  • test2=`convert xc: -format "%[fx:$xb<0?1:0]" info:`
  • if [ $test2 -eq 1 ]; then
    xl=0
    yl=`convert xc: -format "%[fx:($wm1/$hm1)*$xf+$yf]" info:`
  • fi
  • xc=`convert xc: -format "%[fx:round((1-2*($amount*($shorten/100))/100)*$wm1)]" info:`
  • yc=`convert xc: -format "%[fx:round((1-2*($amount*($shorten/100))/100)*$hm1)]" info:`
  • phi1=`convert xc: -format "%[fx:atan(1/(0.01*$curv))]" info:`
  • theta1=`convert xc: -format "%[fx:pi-2*$phi1]" info:`
  • rad1=`convert xc: -format "%[fx:($xr-$xc)/sin($theta1)]" info:`
  • phi2=`convert xc: -format "%[fx:atan(1/(0.01*$curv))]" info:`
  • theta2=`convert xc: -format "%[fx:pi-2*$phi2]" info:`
  • rad2=`convert xc: -format "%[fx:($yb-$yc)/sin($theta2)]" info:`
  • proc="path 'M $xb,$yb A $rad2,$rad2 0 0,0 $xc,$yc A $rad1,$rad1 0 0,0 $xr,$yr Z"
  • dark=`convert xc: -format "%[fx:$dark/100]" info:`
  • bright=$((100-bright))
  • contrast=`convert xc: -format "%[fx:$contrast/100]" info:`
  • hue=`convert xc:$pcolor -colorspace hsb -format "%[fx:100*u.r]" info:`
  • sat=`convert xc:$pcolor -colorspace hsb -format "%[fx:100*u.g]" info:`
  • bri=`convert xc:$pcolor -colorspace hsb -format "%[fx:100*u.b]" info:`
  • bri2=`convert xc: -format "%[fx:$dark*$bri]" info:`
  • pcolor2=`convert xc:"hsb($hue%,$sat%,$bri2%)" -colorspace rgb -format "%[pixel:u.p{0,0}]" info:`
  • if [ $test1 -eq 0 -a $test2 -eq 0 ]; then
    points2="$xb,$yb $xr,$yr $wm1,$hm1"
    elif [ $test1 -eq 1 -a $test2 -eq 0 ]; then
    points2="$xb,$yb $xt,$yt $wm1,0 $wm1,$hm1"
    elif [ $test1 -eq 0 -a $test2 -eq 1 ]; then
    points2="$xl,$yl $xr,$yr $wm1,$hm1 0,$hm1"
    elif [ $test1 -eq 1 -a $test2 -eq 1 ]; then
    points2="$xl,$yl $xt,$yt, $wm1,0 $wm1,$hm1 0,$hm1"
    fi
  • if [ "$bgcolor" = "none" -a "$bgfile" != "" ]; then
    back="$bgfile +swap"
    else
    back="-background $bgcolor"
    fi
  • convert $tmpA1 \
    \( -size ${ww}x${hh} xc:white -fill black -stroke black -draw "polygon $points2" -transparent black $writeA2 \) \
    \( -clone 0 -clone 1 -alpha off -compose copy_opacity -composite -compose over $back -flatten \) \
    \( -size ${ww}x${hh} xc:none -fill black -stroke black -draw "$proc" \) \
    \( -size ${ww}x${hh} xc:black -sparse-color barycentric "$xc,$yc $pcolor $xf,$yf $pcolor2" \
    -evaluate pow $contrast -level 0x$bright% \) \
    \( -clone 4 -clone 3 -compose copy_opacity -composite $writeA4 \) \
    -delete 0,1,3,4 -compose over -composite \
    $outfile