Skip to content

Instantly share code, notes, and snippets.

@lepz0r
Last active March 11, 2021 08:18
Show Gist options
  • Save lepz0r/ebb5e87eb9819d5b99953e0b4324c45b to your computer and use it in GitHub Desktop.
Save lepz0r/ebb5e87eb9819d5b99953e0b4324c45b to your computer and use it in GitHub Desktop.
add a noise to image edges
#!/bin/sh
arg=0
export verbose=0
script_help(){
echo "Usage: coffee-grinder [OPTIONS] "
echo " -r, --radius set edge detection radius (default 2)"
echo " -a, --amount set noise amount (default 25)"
echo " -h, --help show this help"
}
verbose_echo(){
if [ $verbose != 0 ]
then
echo $@
fi
}
while [ -n "$1" ]; do
case "$1" in
-a | --amount)
amount=$2
shift;;
-r | --radius)
blur=$2
shift;;
-h | --help)
script_help
exit;;
-v | --verbose)
export verbose=1;;
*)
case $arg in
0)
input=$1
arg=1;;
1)
output="$1"
arg=2;;
*)
echo "Error: Too many arguments"
script_help
exit;;
esac;;
esac
shift
done
input_filename=$(basename $input)
if [ -z "$amount" ]
then
amount=25
fi
if [ -z "$blur" ]
then
blur=2
fi
resx=$(identify "$input" | grep -Po '[0-9]*?(?=(x))' | head -1)
resy=$(identify "$input" | grep -Po '(?<=(x))[0-9]*' | head -1)
keysize=$(calc -p \($resx\*$resy\))
uuid=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)
tempkey="/tmp/$input_filename.coffee-grinder.$uuid"
umask 066 ; touch $tempkey
verbose_echo "Picture size: $resx"x"$resy"
verbose_echo "Keysize: $keysize"
verbose_echo "Input: $input"
verbose_echo "Output: $output"
verbose_echo "Key Location: $tempkey"
dd if=/dev/urandom of=$tempkey bs=1 count=$keysize status=none
convert -size $resx\x$resy -depth 8 gray:$tempkey $tempkey.png
rm $tempkey
convert -blur $blur $input bmp:- | composite $input bmp:- -compose difference -channel RGB -colorspace Gray PNG24:- | \
convert png24:- -auto-level -channel RGB +level 0%,$amount% -colorspace Gray png:- | \
convert $tempkey.png png:- -alpha off -compose copy_opacity -composite PNG:- | \
composite -compose linear-light -colorspace srgb $input PNG:- $output
rm $tempkey.png
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment