Last active
February 28, 2018 17:14
-
-
Save tedigc/91fe8ba3ad9f1aae825c3ccd822bd410 to your computer and use it in GitHub Desktop.
Bash script for scaling all images in a directory. Includes cropping for ninepatch images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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 | |
OFFSET=$(expr $SCALE - 1) | |
convert $file -scale ${SCALE}00% $NEW_FILENAME | |
convert $NEW_FILENAME -crop +${OFFSET}+${OFFSET} +repage $NEW_FILENAME | |
convert $NEW_FILENAME -crop -${OFFSET}-${OFFSET} +repage $NEW_FILENAME | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment