Skip to content

Instantly share code, notes, and snippets.

@pax
Created January 27, 2024 21:23
Show Gist options
  • Save pax/29531e260ec476af078c7c11cef4c725 to your computer and use it in GitHub Desktop.
Save pax/29531e260ec476af078c7c11cef4c725 to your computer and use it in GitHub Desktop.
batch resize and optimize images
#!/bin/bash
# Check if an argument is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 /path/to/image/directory"
exit 1
fi
# Define the maximum width, height, and quality
MAX_WIDTH=1920
MAX_HEIGHT=1080
QUALITY=85 # Adjust quality percentage (100 = highest quality)
# Directory containing images, passed as an argument
IMAGE_DIR=$1
# Loop through all jpg and png images in the directory and its subdirectories
find "$IMAGE_DIR" -type f \( -iname '*.jpg' -o -iname '*.png' \) | while read -r img; do
# Resize images over the maximum dimensions and compress
mogrify -resize "${MAX_WIDTH}x${MAX_HEIGHT}>" -quality $QUALITY $img
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment