Skip to content

Instantly share code, notes, and snippets.

@ralphtheninja
Created August 17, 2013 23:57
Show Gist options
  • Select an option

  • Save ralphtheninja/6259239 to your computer and use it in GitHub Desktop.

Select an option

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
#!/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}
@ralphtheninja
Copy link
Author

Needs ImageMagick to be installed for convert and identify commands to work.

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