utils/watermark.sh
Irrlicht 3e888dbffa
[watermark] Add a mode to watermark with a text. General improvements.
Add the `-x` option to use a string passed as a parameter to watermark
inputs.

Improvements: use functions.
2026-10-12 10:13:15 +02:00

104 lines
2.3 KiB
Shell
Executable file

#!/bin/bash
COMPOSITE="/usr/bin/composite"
MAGICK="/usr/bin/magick"
MODE_IMG=1 # binary 001
MODE_TXT=2 # binary 010
mode=$MODE_IMG
INPUTS=()
TRANSPARENCY=40
while [ $# -gt 0 ]
do
case $1 in
-t|--transparency)
shift
if [ $# -eq 0 ]; then
echo "You must specify a transparency value between 0 and 100."
exit 1
fi
TRANSPARENCY=$1
shift
;;
-x|--text)
shift
if [ $# -eq 0 ]; then
echo "You must specify a text for the watermark."
exit 1
fi
watermark_text=$1
mode=$MODE_TXT
shift
;;
-w|--watermark)
shift
if [ $# -eq 0 ]; then
echo "You must specify path to watermark file"
exit 1
fi
WATERMARK="$1"
shift
;;
*)
INPUTS+=("$1")
shift
;;
esac
done
FILE_PATH=
EXTENSION=
apply_watermark_text() {
local outpath
local file_path
local extension
for i in ${INPUTS[@]}; do
echo "file: $i"
file_path=${i%.*}
extension=${i##*.}
outpath="$file_path-watermark.$extension"
magick \
-background None -fill "#fff7" \
-font "IBM-Plex-Serif" \
-pointsize 20 label:"$watermark_text" \
-rotate -40 +repage \
-write mpr:TILE +delete \
\( ${i}[0] -fill mpr:TILE \
-draw 'color 0,0 reset' -write mpr:TILES +delete \) \
${i} -coalesce NULL: \
mpr:TILES \
-compose over -layers composite \
"${outpath}"
done
}
if [ $(($mode & $MODE_IMG)) -eq $mode ]; then
echo "Mode: image"
if [ -z $WATERMARK ]; then
echo "No watermark"
exit
fi
for i in ${INPUTS[@]}
do
file_path=${i%.*}
extension=${i##*.}
outpath="$file_path-watermark.$extension"
if [ ! -f $outpath ]; then
$COMPOSITE -gravity SouthEast -dissolve $TRANSPARENCY "$WATERMARK" "$i" "$outpath"
fi
done
elif [ $(($mode & $MODE_TXT)) -eq $mode ]; then
echo "Mode: text"
apply_watermark_text
else
echo "Unknown mode"
exit 1
fi
exit 124