Skip to content

Instantly share code, notes, and snippets.

@DRRDietrich
Created June 26, 2020 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DRRDietrich/1e49d9411d4144edc6a2a842fef541b0 to your computer and use it in GitHub Desktop.
Save DRRDietrich/1e49d9411d4144edc6a2a842fef541b0 to your computer and use it in GitHub Desktop.
Script to apply watermarks on images
#!/bin/bash
config="${HOME}/.config/watermark"
usage() { echo "Usage: $0 [-e <jpeg|jpg|png>] [-n <watermark output name>] [-o <output path>] [-p <watermark position>] [-t <transparency>] [-w <watermark path>] picture [picture [...]]" 1>&2; exit 1; }
if [[ ! -d "$config" ]]; then
mkdir -p "$config"
touch "$config/watermark.cfg"
fi
resultpath="watermark"
watermarkname="watermark"
watermarkextension="png"
transparency="0.15"
position="center"
source "$config/watermark.cfg"
while getopts ":e:n:o:p:t:w:" opt; do
case $opt in
e) watermarkextension="${OPTARG}";;
n) watermarkname="${OPTARG}";;
o) resultpath="${OPTARG}";;
p) position="${OPTARG}";;
t) transparency="${OPTARG}";;
w) watermark="${OPTARG}";;
\?) echo "Invalid option: -$OPTARG"; usage; >&2; exit 1;;
:) echo "Option -$OPTARG requires an argument." >&2; exit 1;;
esac
done
shift $((OPTIND-1))
if [[ -z "${watermark}" ]]; then
echo "A watermark must be specified."; usage;
fi
mkdir -p "${resultpath}"
for image in "$@"
do
imagewidth=$(identify -format "%[fx:w]" "${image}")
imageheight=$(identify -format "%[fx:h]" "${image}")
watermarkpath="${resultpath}/${watermarkname}_${imagewidth}_${imageheight}.${watermarkextension}"
if [[ ! -s ${watermarkpath} ]]; then
convert -resize "${imagewidth}x${imageheight}" -alpha set -background none -channel A -evaluate multiply "$transparency" +channel "${watermark}" "${watermarkpath}"
fi;
composite -gravity "${position}" "${watermarkpath}" "${image}" "${resultpath}/${image##*/}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment