Skip to content

Instantly share code, notes, and snippets.

@robsonke
Last active September 10, 2016 12:15
Show Gist options
  • Save robsonke/1b0e06e060e9836e6fb88e3e0e6eabde to your computer and use it in GitHub Desktop.
Save robsonke/1b0e06e060e9836e6fb88e3e0e6eabde to your computer and use it in GitHub Desktop.
Resizes and renames image files in the current directory using sips (osx). Can be rewritten to imagemagick easily to make it work on Linux too.
# Given a collection of image files, this function will resize
# the images in the current folder and saves them in the subfolder resized
#
# Usage: resize-images
# Note: the read command is specific for ZSH shells, for Bash, use -p param. Same for the setopt, it's shopt for bash.
#
## Resize and rename images in the current directory and writes them to a resized subfolder
function resize-images() {
read "filename?What would you like the filename to be started with? []: "
read "width?What max width in pixels would you like your images? [1024]: "
read "height?What max height in pixels would you like your images? [768]: "
width=${width:-1024}
height=${height:-768}
echo
echo "All images will be renamed to $filename-xx.jpg with a maximum resolution of $width x $height pixels."
echo
mkdir -p resized
# start renaming and resizing the images
count=1
total=$(find . -type f \( -name "*.jpg" -or -name "*.jpeg" -or -name "*.JPG" -or -name "*.JPEG" \) -maxdepth 1 | wc -l | sed 's/ //g')
# shopt in bash
setopt nullglob nocaseglob
for file in *.jpg *.jpeg
do
# rename
new=$(printf "${filename}_%03d.jpg" $count)
echo
echo "($count/$total) Processing $file and renaming to $new..."
echo
cp $file $new
# resize and move
sips --resampleHeightWidth $height $width --setProperty formatOptions 80 $new --out resized/
# remove temp file
rm $new
let count=count+1
done
# open the directory in Finder
open resized/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment