Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Quickly make image(s) a given dimension by changing canvas size, not scaling.
# Make the given file(s) the given dimension, by extending the canvas, rather
# than scaling. Made especially for PNGs, since it makes the background
# transparent and all. This programmer in particular has found it useful for
# different-sized icons that end at their exact boundaries, causing headaches
# when developing Android apps.
# Name some args
# Gotta have some parameters to continue
# First parameter should be image dimensions, i.e. contain 'x' in the middle.
if [ -z "$FINALSIZE" ] || [[ $FINALSIZE != *x* ]]; then
if [[ $FINALSIZE != *x* ]]; then
echo "desired-size not given!"
echo "usage: desired-size file [file2 ...]"
echo " desired-size: Desired image dimensions, e.g. 164x120"
exit 1
# Make sure we've got the magic butter
hash convert 2>/dev/null || { echo >&2 "MISSING convert COMMAND! INSTALL ImageMagick TO CONTINUE."; echo; exit 1; }
for INFILE in ${@:2}; do
FILENAME=$(basename $INFILE)
# Do the stuff
# If your grays are fine, use this. Otherwise comment out, and use command below.
convert $INFILE -background transparent -gravity center -extent $FINALSIZE $FILENAME
# Sometimes this script messes up grays in your PNGs. If so, uncomment this:
#convert $INFILE -define png:big-depth=16 -define png:color-type=6 -background transparent -gravity center -extent $FINALSIZE $FILENAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.