Skip to content

Instantly share code, notes, and snippets.

@nicolas-cusan
Last active November 3, 2021 09:27
Show Gist options
  • Save nicolas-cusan/76cf79370c59db8c604edcc0b34eaca6 to your computer and use it in GitHub Desktop.
Save nicolas-cusan/76cf79370c59db8c604edcc0b34eaca6 to your computer and use it in GitHub Desktop.
This script finds all the images in a folder (and its subfolders) that are larger than 3MB, resizes them to have a maximum size of 3000px on the longest side and replaces the original image file.
#!/bin/bash
# This script finds all the images in a folder (and its subfolders) that are larger than 3MB,
# resizes them to have a maximum size of 3000px on the longest side and replaces the original image file.
# Usage: > sh ./batch-resize-mac.sh ./path/to/folder
# Info: https://ss64.com/osx/sips.html
# Credit: https://gist.github.com/lopespm/893f323a04fcc59466d7
initial_folder="$1"
all_images=$(find -E $initial_folder -iregex ".*\.(jpg|gif|png|jpeg)")
max_size=3000000
while read -r image_full_path; do
if [ ! -z "$image_full_path" -a "$image_full_path" != " " ]; then
file_size=$(wc -c "$image_full_path" | awk '{print $1}')
if (( file_size > max_size)); then
sips -Z 3000 "$image_full_path" --out "$image_full_path";
fi
fi
done <<< "$all_images"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment