#!/bin/bash abs() { [[ $[ $@ ] -lt 0 ]] && echo "$[ ($@) * -1 ]" || echo "$[ $@ ]" } DIMENSIONS=( $(identify "${1}" | cut -d\ -f3 | sed "s/x/ /g") ) POWERS=( 16 32 64 128 ) for POWER in ${POWERS[@]}; do WIDEDIFS+=( $(abs "${DIMENSIONS[0]}-${POWER}") ) HITEDIFS+=( $(abs "${DIMENSIONS[1]}-${POWER}") ) done IFS=$'\n' WIDESORT=( $(sort -n <<<"${WIDEDIFS[*]}")) HITESORT=( $(sort -n <<<"${HITEDIFS[*]}")) unset IFS for SIZE in $((DIMENSIONS[0] - WIDESORT[0])) $((DIMENSIONS[0] + WIDESORT[0])); do (( SIZE > 0 && (SIZE & (SIZE - 1)) == 0 )) && RESIZE=${SIZE} done TEMPFILE=$(mktemp) convert "${1}" -resize ${RESIZE}x${RESIZE}\! ${TEMPFILE} png2icns ${1/%png/icns} ${TEMPFILE} rm ${TEMPFILE}