Skip to content

Instantly share code, notes, and snippets.

@dlo
Last active November 9, 2015 22:11
Show Gist options
  • Save dlo/49946c22a9b40308889a to your computer and use it in GitHub Desktop.
Save dlo/49946c22a9b40308889a to your computer and use it in GitHub Desktop.
Turn @2x images into @1x on the command line.

This is a simple Bash function that will (should?) turn any @2x image into a non-retina version.

Usage

Place the contents of deretinize.sh in your .bashrc, .bash_profile, or any other file that will get executed when you load your shell. Then,

$ deretinize xyz@2x.png

This will output a file named xyz.png in the same directory. If the original image has odd dimensions, the script will resize it appropriately.

function deretinize() {
INPUT=$1
DIMENSIONS=`dimensions $INPUT`
GRAVITY=""
UPDATED_DIMENSIONS=""
HALF_DIMENSIONS=""
INDEX=0
for VALUE in `echo $DIMENSIONS | tr "x" "\n"`; do
if [ $INDEX -eq 0 ]; then
DIRECTION="west";
else
DIRECTION="south";
UPDATED_DIMENSIONS+="x"
HALF_DIMENSIONS+="x"
fi
if [ $(( VALUE % 2 )) -eq 1 ]; then
GRAVITY+=$DIRECTION
UPDATED_DIMENSIONS+=$((VALUE+1))
HALF_DIMENSIONS+=$(((VALUE+1) / 2))
else
UPDATED_DIMENSIONS+=$((VALUE))
HALF_DIMENSIONS+=$((VALUE/2))
fi
INDEX=$((INDEX+1))
done
if [ -z "$GRAVITY" ]; then
GRAVITY="center"
fi
if [ "$UPDATED_DIMENSIONS" != "$DIMENSIONS" ]; then
convert $INPUT -background transparent -gravity $GRAVITY -extent $UPDATED_DIMENSIONS $INPUT
fi
OUTPUT=`echo $INPUT | sed -E "s/@2x\.(\w*)/.\1/g"`
convert $INPUT -resize $HALF_DIMENSIONS $OUTPUT
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment