Skip to content

Instantly share code, notes, and snippets.

@tsuharesu
Last active June 13, 2025 19:35
Show Gist options
  • Save tsuharesu/dfe751e3a79a65b0ff7509cea07d821f to your computer and use it in GitHub Desktop.
Save tsuharesu/dfe751e3a79a65b0ff7509cea07d821f to your computer and use it in GitHub Desktop.
Image edit with ImageMagick
#!/bin/bash
# This script processes photos in a specified input directory, applying a blur effect, resizing, adding a border, and overlaying a watermark.
# Requirements: ImageMagick must be installed and accessible via the 'magick' command.
# Receive the input directory as a parameter or use the default value.
INPUT_DIR="${1:-input}"
# Ensure the input directory exists
if [ ! -d "$INPUT_DIR" ]; then
echo "Input directory '$INPUT_DIR' does not exist."
exit 1
fi
RESIZE_DIMENSIONS="1080x1080"
BLUR_RADIUS="0x50" # Standard deviation (0 for auto-radius) x Sigma (how much blur)
# Adjust Sigma (e.g., 5, 10, 15) for more or less blur
BORDER_SIZE="8" # Border thickness in pixels for the original image
BORDER_COLOR="white" # Color of the border
WATERMARK_IMAGE="${2:-watermark.png}"
WATERMARK_SIZE="100x100"
WATERMARK_OFFSET="+0+0" # Horizontal + Vertical offset from gravity (e.g., +20+20 for 20px from right/bottom)
OUTPUT_DIR="${INPUT_DIR}_output"
mkdir -p "$OUTPUT_DIR"
for photo in "$INPUT_DIR"/*.{jpg,jpeg}; do
if [ -f "$photo" ]; then
filename=$(basename "$photo")
output_path="$OUTPUT_DIR/$filename"
echo "Processing $filename..."
magick "$photo" \
-write mpr:original \
-blur "$BLUR_RADIUS" \
-resize "${RESIZE_DIMENSIONS}^" \
-gravity center \
-extent "$RESIZE_DIMENSIONS" \
\( mpr:original \
-shave "${BORDER_SIZE}x${BORDER_SIZE}" \
-bordercolor "$BORDER_COLOR" \
-border "${BORDER_SIZE}" \
-resize "${RESIZE_DIMENSIONS}>" \) \
-gravity center \
-composite \
\( "$WATERMARK_IMAGE" -resize "${WATERMARK_SIZE}>" \
-channel A -evaluate multiply 0.5 +channel \) \
-gravity southeast \
-geometry "$WATERMARK_OFFSET" \
-composite \
"$output_path"
fi
done
echo "All photos processed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment