Skip to content

Instantly share code, notes, and snippets.

@funnyzak
Last active March 16, 2024 11:04
Show Gist options
  • Save funnyzak/1c3d0180d290188c943b8a8799e5c732 to your computer and use it in GitHub Desktop.
Save funnyzak/1c3d0180d290188c943b8a8799e5c732 to your computer and use it in GitHub Desktop.
A Bash script for adding a watermark to all image files (.png, .jpg, .jpeg) in a specified directory.
#!/bin/bash
# Check the number of arguments
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <path to watermark image> <search location> <dissolve percentage>"
exit 1
fi
# Path to the watermark image
watermark_path="$1"
# Search location
search_location="$2"
# Dissolve percentage for transparency
dissolve_percentage="$3"
if ! [[ "$dissolve_percentage" =~ ^[0-9]+$ ]]; then
echo "Dissolve percentage must be a number"
exit 1
fi
if [ "$dissolve_percentage" -lt 0 ] || [ "$dissolve_percentage" -gt 100 ]; then
echo "Dissolve percentage must be between 0 and 100"
exit 1
fi
# Iterate over all image files in the directory
find "$search_location" -type f \( -iname "*.png" -o -iname "*.jpg" -o -iname "*.jpeg" \) -print0 | while IFS= read -r -d '' file; do
# Get the filename and extension
filename="${file%.*}"
extension="${file##*.}"
# Add a watermark to the image
magick composite -dissolve "$dissolve_percentage" -gravity southwest -geometry +15+15 "$watermark_path" "$file" "$filename"_wm.$extension
if [ $? -ne 0 ]; then
echo "Failed to watermark $file" >> wm.log
else
echo "Watermarked $file, output to $filename"_wm.$extension""
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment