Skip to content

Instantly share code, notes, and snippets.

@elgreg
Created December 19, 2017 04:06
Show Gist options
  • Save elgreg/ddbabd3a077985c6a5fdd050f07c783d to your computer and use it in GitHub Desktop.
Save elgreg/ddbabd3a077985c6a5fdd050f07c783d to your computer and use it in GitHub Desktop.
#! /bin/bash
# Requires imagemagick (brew install imagemagick)
# Usage:
# ./pad_to_3_x_2.sh [image-name]
#
# produces resized-[image-name]
#
# Example:
#
# ./pad_to_3_x_2.sh screenshot_whut.png
#
ORIGINAL_DIMENSIONS=`magick identify $1 | cut -d " " -f3`;
WIDTH=`echo -n $ORIGINAL_DIMENSIONS | cut -d"x" -f1`
HEIGHT=`echo -n $ORIGINAL_DIMENSIONS | cut -d"x" -f2`
NEWWIDTH=$WIDTH
NEWHEIGHT=$HEIGHT
if [ "$WIDTH" -ge "$HEIGHT" ]
then
NEWHEIGHT=$(($WIDTH * 2 / 3))
else
NEWWIDTH=$(($HEIGHT * 2 / 3))
fi
echo "Original Width: $WIDTH"
echo "Original Height: $HEIGHT"
echo "New Width: $NEWWIDTH"
echo "New Height: $NEWHEIGHT"
echo -n "Resizing to ${NEWWIDTH}x${NEWHEIGHT}"
NEWDIMS="${NEWWIDTH}x${NEWHEIGHT}"
convert $1 -gravity center -background transparent -extent $NEWDIMS resized-$1
open "resized-$1";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment