Skip to content

Instantly share code, notes, and snippets.

@maojj
Created June 3, 2013 05:40
Show Gist options
  • Save maojj/5696284 to your computer and use it in GitHub Desktop.
Save maojj/5696284 to your computer and use it in GitHub Desktop.
#! /bin/bash
# File name : convertImage.sh
# Author: Mao Jiajun
#
# print usage
usage() {
cat << EOF
Usage:
cropcorner.sh <> <src file> <geometry> <dest file>
ex,
EOF
}
if [ $# -ne 3 ]; then
usage
exit 1
fi
SRC_FILE=$1
DES_FILE=$3
DES_SIZE=$2
DES_WIDTH=`echo "$DES_SIZE" | cut -f1 -d'x'`
DES_HEIGHT=`echo "$DES_SIZE" | cut -f2 -d'x'`
W1=$((DES_WIDTH / 2))
W2=$((DES_WIDTH - W1))
H1=$((DES_HEIGHT / 2))
H2=$((DES_HEIGHT - H1))
echo $W1 $W2 $H1 $H2
# check src dir
if [ ! -f $SRC_FILE ]; then
echo "src file not exist: $SRC_FILE"
# exit 1
fi
SRC_FILE_INFO=`identify $SRC_FILE`
SRC_SIZE=`echo "$SRC_FILE_INFO" | cut -f3 -d' '`
SRC_WIDTH=`echo "$SRC_SIZE" | cut -f1 -d 'x'`
SRC_HEIGHT=`echo "$SRC_SIZE" | cut -f2 -d 'x'`
FILE_EXTENSION=${DES_FILE##*.}
FILE_NAME=${DES_FILE%.*}
TEMP_FILE_LU="${FILE_NAME}lu.$FILE_EXTENSION"
TEMP_FILE_LD="${FILE_NAME}ld.$FILE_EXTENSION"
TEMP_FILE_RU="${FILE_NAME}ru.$FILE_EXTENSION"
TEMP_FILE_RD="${FILE_NAME}rd.$FILE_EXTENSION"
TEMP_FILE_L="${FILE_NAME}l.$FILE_EXTENSION"
TEMP_FILE_R="${FILE_NAME}r.$FILE_EXTENSION"
TEMP_FILE_U="${FILE_NAME}u.$FILE_EXTENSION"
TEMP_FILE_D="${FILE_NAME}d.$FILE_EXTENSION"
if [ $DES_WIDTH -gt $SRC_WIDTH ]; then
echo "$DES_SIZE is too wide to $SRC_SIZE"
exit 1
fi
if [ $DES_HEIGHT -gt $SRC_HEIGHT ]; then
echo "$DES_SIZE is too high"
exit 1
fi
if [ $DES_WIDTH -eq $SRC_WIDTH ]; then
echo "same width, crop up and buttom"
exit 1
fi
if [ $DES_HEIGHT -eq $SRC_HEIGHT ]; then
echo "same height, crop leght and right"
exit 1
fi
echo "crop four images in four corners"
# cut four corner images to merge
convert -crop ${W1}x${H1}+0+0 $SRC_FILE $TEMP_FILE_LU
convert -crop ${W1}x${H2}+0+$(($SRC_HEIGHT - $H2)) $SRC_FILE $TEMP_FILE_LD
convert -crop ${W2}x${H1}+$(($SRC_WIDTH - $W2))+0 $SRC_FILE $TEMP_FILE_RU
convert -crop ${W2}x${H2}+$(($SRC_WIDTH - $W2))+$(($SRC_HEIGHT - $H2)) $SRC_FILE $TEMP_FILE_RD
convert $TEMP_FILE_LU $TEMP_FILE_RU +append $TEMP_FILE_U
convert $TEMP_FILE_LD $TEMP_FILE_RD +append $TEMP_FILE_D
convert $TEMP_FILE_U $TEMP_FILE_D -append $DES_FILE
rm $TEMP_FILE_LU
rm $TEMP_FILE_LD
rm $TEMP_FILE_RU
rm $TEMP_FILE_RD
rm $TEMP_FILE_U
rm $TEMP_FILE_D
echo "done! Have Fun!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment