Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A bash script to batch resize images on macOS, possibly through an Automator Service (uses sips, ImageOptim)
for f in "$@"
do
dir=$(dirname "${f}")
filename=$(basename -- "$f")
extension="${filename##*.}"
filename="${filename%.*}"
# array of pixel sizes (widths) to generate
sizes=(600 1200)
for s in "${sizes[@]}"
do
newfilename="$dir"/"$filename"_"${s}"."$extension"
cp "$f" "$newfilename" # make a copy of the file first
sips -Z "${s}" -s formatOptions 60 "$newfilename" # resize the copy
/Applications/ImageOptim.app/Contents/MacOS/ImageOptim "$newfilename" # run imageoptim on the resized image
done
done
osascript -e 'display notification "Image resizing complete!"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment