Nativefier/bin/pngToIcns

104 lines
3.1 KiB
Plaintext
Raw Normal View History

#!/bin/sh
# modified from https://github.com/daveish/png2icns for OSX 10.11
# USAGE
# ./pngToIcns <input png> <outp icns>
# Example
# ./pngToIcns.sh ~/sample.png ~/Desktop/converted.icns
# Exec Paths
ICONUTIL=$(which iconutil)
2016-03-09 07:22:11 +01:00
IMAGEMAGICK_CONVERT=$(which convert)
IMAGEMAGICK_IDENTIFY=$(which identify)
if [ ! -x "${ICONUTIL}" ]; then
echo "Cannot find required iconutil executable" >&2
exit 1;
fi
2016-03-09 07:22:11 +01:00
if [ ! -x "${IMAGEMAGICK_CONVERT}" ]; then
echo "Cannot find required ImageMagick Convert executable" >&2
exit 1;
fi
2016-03-09 07:22:11 +01:00
if [ ! -x "${IMAGEMAGICK_IDENTIFY}" ]; then
echo "Cannot find required ImageMagick Identify executable" >&2
exit 1;
fi
# Parameters
SOURCE=$1
2016-02-04 07:43:08 +01:00
DEST=$2
# Check source and destination arguments
if [ -z "${SOURCE}" ]; then
echo "No source image specified"
exit 1
fi
2016-02-04 07:43:08 +01:00
if [ -z "${DEST}" ]; then
echo "No destination specified"
exit 1
fi
# File Infrastructure
NAME=$(basename "${SOURCE}")
EXT="${NAME##*.}"
BASE="${NAME%.*}"
ICONSET="${BASE}.iconset"
mkdir "${ICONSET}"
# check if .ico is a sequence
IS_ICO_SET="$(identify ${SOURCE} | grep -e "\w\.ico\[0")"
if [ "${IS_ICO_SET}" ]; then
# extract the largest(?) image from the set
convert "${SOURCE}" "${ICONSET}/${BASE}.png"
SOURCE="${ICONSET}/${BASE}-0.png"
fi
# Debug Info
# echo "SOURCE: ${SOURCE}"
# echo "NAME: $NAME"
# echo "BASE: $BASE"
# echo "EXT: $EXT"
# echo "ICONSET: $ICONSET"
# Get source image info
2016-02-04 07:43:08 +01:00
#SRCWIDTH=$( sips -g pixelWidth "${SOURCE}" | tail -n1 | awk '{print $2}')
#SRCHEIGHT=$( sips -g pixelHeight "${SOURCE}" | tail -n1 | awk '{print $2}' )
#SRCFORMAT=$( sips -g format "${SOURCE}" | tail -n1 | awk '{print $2}' )
# Debug Info
# echo "SRCWIDTH: $SRCWIDTH"
# echo "SRCHEIGHT: $SRCHEIGHT"
# echo "SRCFORMAT: $SRCFORMAT"
2016-02-04 07:43:08 +01:00
#
#if [ "x${SRCFORMAT}" != "xpng" ]; then
# echo "ERR: Source image format should be png." >&2
# exit 1;
#fi
# Resample image into iconset
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 16x16 "${ICONSET}/icon_16x16.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 32x32 "${ICONSET}/icon_16x16@2x.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 32x32 "${ICONSET}/icon_32x32.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 64x64 "${ICONSET}/icon_32x32@2x.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 128x128 "${ICONSET}/icon_128x128.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 256x256 "${ICONSET}/icon_128x128@2x.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 256x256 "${ICONSET}/icon_256x256.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 512x512 "${ICONSET}/icon_256x256@2x.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 512x512 "${ICONSET}/icon_512x512.png"
convert "${SOURCE}" -define png:big-depth=16 -define png:color-type=6 -sample 1024x1024 "${ICONSET}/icon_512x512@2x.png"
# Create an icns file lefrom the iconset
2016-03-09 07:22:11 +01:00
iconutil -c icns "${ICONSET}" -o "${DEST}"
# Clean up the iconset
rm -rf "${ICONSET}"