Skip to content

Instantly share code, notes, and snippets.

@twlz0ne
Created February 18, 2018 05:01
Show Gist options
  • Save twlz0ne/10d02d38a37a2ffe61c557a852ce8fb4 to your computer and use it in GitHub Desktop.
Save twlz0ne/10d02d38a37a2ffe61c557a852ce8fb4 to your computer and use it in GitHub Desktop.
Add shadow to image
#!/bin/bash
##
# Add shadow to image
# @author [email protected]
# @date 2014-11-22
##
usage() {
app=$(basename $0)
cat <<EOF
Usage: $app input_file {percent-opacity}x{sigma}+{bottom}+{right}-{left}-{top}
Options:
percent-opacity default 60
sigma default 3
bottom bottom shadow, default 3
right right shadow, default 3
left left shadow, default 0
top top shadow, default 0
Example:
# Shadow at bottom & right, same as '$app example.png 60x3+3+3'.
$app example.png
# Shadow at left & top.
$app example.png 60x3+0+0-3-3
EOF
exit $1
}
test $# -lt 1 && usage 1
in=$1
def_opacity=60
def_sigma=3
def_shadow_br=3
def_shadow_lt=0
options=$(echo $2 | awk -F 'x|+|-' '{print $1,$2,$3,$4,$5,$6}')
set -- $options
[[ -z $1 ]] && opt_opacity=$def_opacity || opt_opacity=$1
[[ -z $2 ]] && opt_sigma=$def_sigma || opt_sigma=$2
if [[ -z $1 ]] || [[ -z $2 ]]; then
opt_shadow_b=$def_shadow_br
opt_shadow_r=$def_shadow_br
opt_shadow_l=0
opt_shadow_t=0
else
opt_shadow_b=${3:-0}
opt_shadow_r=${4:-0}
opt_shadow_l=${5:-0}
opt_shadow_t=${6:-0}
fi
out=${in%.*}-shadow_+$opt_shadow_b+$opt_shadow_r-$opt_shadow_l-$opt_shadow_t.${in#*.}
echo "Created file: $out"
opt_shadow_groups=()
if [[ $opt_shadow_r != 0 ]] || [[ $opt_shadow_b != 0 ]]; then
opt_shadow_groups+=(+${opt_shadow_r}+${opt_shadow_b})
fi
if [[ $opt_shadow_l != 0 ]] || [[ $opt_shadow_t != 0 ]]; then
opt_shadow_groups+=(-${opt_shadow_l}-${opt_shadow_t})
fi
for (( i=0; i<${#opt_shadow_groups[@]}; i++ )); do
convert $((( $i <= 0)) && echo "$in" || echo "$out") \
\( +clone -background black -shadow ${opt_opacity}x${opt_sigma}${opt_shadow_groups[$i]} \) \
-reverse -background none -layers merge +repage "$out"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment