Created
August 17, 2013 23:57
-
-
Save ralphtheninja/6259239 to your computer and use it in GitHub Desktop.
./crop.sh foo.png 100 100 out.png Crops foo.png by a centered 100 100 rect and stores the result in out.png
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
| #!/bin/bash | |
| IN_FILE="$1" | |
| OUT_FILE="$4" | |
| CROP_W=$2 | |
| CROP_H=$3 | |
| SIZE=$(identify $IN_FILE | awk '{print $3}') | |
| IMAGE_W=${SIZE%%x*} | |
| IMAGE_H=${SIZE##*x} | |
| let X=($IMAGE_W-$CROP_W)/2 | |
| let Y=($IMAGE_H-$CROP_H)/2 | |
| CROP="-crop ${CROP_W}x${CROP_H}" | |
| if (( "$X" > 0)); then | |
| CROP+="+$X" | |
| else | |
| CROP+="$X" | |
| fi | |
| if (( "$Y" > 0)); then | |
| CROP+="+$Y" | |
| else | |
| CROP+="$Y" | |
| fi | |
| convert ${IN_FILE} ${CROP} ${OUT_FILE} |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Needs ImageMagick to be installed for convert and identify commands to work.