Skip to content

Instantly share code, notes, and snippets.

@joshuaconner
Last active December 13, 2015 16:39
Show Gist options
  • Save joshuaconner/4941992 to your computer and use it in GitHub Desktop.
Save joshuaconner/4941992 to your computer and use it in GitHub Desktop.
Copy this into your .bash_profile! Bash function to batch-resize images according to the geometry specified in resize-arg. If no destination directory is specified, the converted images' filenames have "-resized" appended so as to not overwrite the original images.
resize () {
SUFFIX="-resized"
if [ -n "$1" ] ; then
if [ ! -f "${@: -1}" ] ; then
SUFFIX=""
DEST="${@: -1}"
mkdir -p "$DEST"
fi
geom="$1"
shift
while [ -n "$1" ]
do
if [ -f "$1" ]; then
FILENAME=$(basename "$1")
EXTENSION="${FILENAME##*.}"
FILENAME="${FILENAME%.*}"
convert -filter Lanczos2 -resize "$geom" "$1" "${DEST}/${FILENAME}${SUFFIX}.${EXTENSION}"
fi
shift
done
else
echo "Usage: resize resize-arg [file1, file2, ...] destinationDirectory"
echo "For possible resize-arg formats, see:"
echo "http://www.imagemagick.org/script/command-line-processing.php#geometry"
echo ""
echo 'If a destination directory is no specified, each filename will be appended with'
echo '"-resized" so as to not overwrite the original files.'
fi
}
@joshuaconner
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment