Skip to content

Instantly share code, notes, and snippets.

@tedigc
Created February 28, 2018 15:04
Show Gist options
  • Save tedigc/5c65056994be3c1493669b7c156eb0e0 to your computer and use it in GitHub Desktop.
Save tedigc/5c65056994be3c1493669b7c156eb0e0 to your computer and use it in GitHub Desktop.
Bash script for batch-scaling images in a directory.
#!/bin/bash
#
# Check that the correct number of arguments have been given.
#
if [ "$#" -ne 3 ]; then
echo
echo "[Error]: Usage \"./scale.sh <input_dir> <output_dir> <scale>\""
echo
exit 1
fi
INPUT_DIR=$1
OUTPUT_DIR=$2
SCALE=$3
#
# Check that the input directory exists.
#
if [ ! -d "$INPUT_DIR" ]; then
echo
echo "[Error]: Cannot find directory \"$INPUT_DIR\""
echo
fi
#
# If the output directory does not exist, then create it.
#
if [ ! -d "$OUTPUT_DIR" ]; then
echo
echo "Directory \"$OUTPUT_DIR\" not found. Creating empty directory..."
echo
mkdir $OUTPUT_DIR
fi
#
# Check the third parameter is a positive integer.
#
re='^[0-9]+$'
if ! [[ $SCALE =~ $re ]] ; then
echo "[Error]: <scale> should be a positive integer." >&2; exit 1
fi
#
# Scale each .png file.
#
for file in "$INPUT_DIR"*.png
do
NEW_FILENAME="$OUTPUT_DIR$(basename $file)"
echo "Scaling and cropping $NEW_FILENAME"
convert $file -scale "${SCALE}00%" $NEW_FILENAME
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment