#!/bin/bash abs() { [[ $[ $@ ] -lt 0 ]] && echo "$[ ($@) * -1 ]" || echo "$[ $@ ]" } DIMS=( $(identify "${1}" | cut -d\ -f3 | sed "s/x/ /g") ) PWRS=( 16 32 128 256 512 1024 ) for PWR in ${PWRS[@]}; do WIDEDIFS+=( $(abs "${DIMS[0]}-${PWR}") ) HITEDIFS+=( $(abs "${DIMS[1]}-${PWR}") ) done IFS=$'\n' WIDESORT=( $(sort -n <<<"${WIDEDIFS[*]}")) HITESORT=( $(sort -n <<<"${HITEDIFS[*]}")) unset IFS for SIZE in $((DIMS[0] - WIDESORT[0])) $((DIMS[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}