From ca44ee28aaee1b1b88dc1952cf0679f974d3f3c2 Mon Sep 17 00:00:00 2001 From: Jia Hao Date: Thu, 10 Mar 2016 15:41:25 +0800 Subject: [PATCH] Use separate file to convert to png --- bin/convertToPng | 68 ++++++++++++++++++++++++++++++++++++++++++ bin/pngToIcns | 77 ++++++++++++++++++------------------------------ 2 files changed, 97 insertions(+), 48 deletions(-) create mode 100755 bin/convertToPng diff --git a/bin/convertToPng b/bin/convertToPng new file mode 100755 index 0000000..4ce7668 --- /dev/null +++ b/bin/convertToPng @@ -0,0 +1,68 @@ +#!/bin/sh + +# USAGE + +# ./convertToPng .png +# Example +# ./convertToPng ~/sample.ico ~/Desktop/converted.png + +set -e + +# Exec Paths +IMAGEMAGICK_CONVERT=$(which convert) +IMAGEMAGICK_IDENTIFY=$(which identify) + +if [ ! -x "${IMAGEMAGICK_CONVERT}" ]; then + echo "Cannot find required ImageMagick Convert executable" >&2 + exit 1; +fi +if [ ! -x "${IMAGEMAGICK_IDENTIFY}" ]; then + echo "Cannot find required ImageMagick Identify executable" >&2 + exit 1; +fi + +# Parameters +SOURCE=$1 +DEST=$2 + +# Check source and destination arguments +if [ -z "${SOURCE}" ]; then + echo "No source image specified" + exit 1 +fi + +if [ -z "${DEST}" ]; then + echo "No destination specified" + exit 1 +fi + +# File Infrastructure +NAME=$(basename "${SOURCE}") +EXT="${NAME##*.}" +BASE="${NAME%.*}" +TEMP_DIR="convert_temp" + +function cleanUp() { + rm -rf "${TEMP_DIR}" +} + +trap cleanUp EXIT + +mkdir -p "${TEMP_DIR}" + +# check if .ico is a sequence +# pipe into cat so no exit code is given for grep if no matches are found +IS_ICO_SET="$(identify ${SOURCE} | grep -e "\w\.ico\[0" | cat )" + +convert "${SOURCE}" "${TEMP_DIR}/${BASE}.png" +if [ "${IS_ICO_SET}" ]; then + # extract the largest(?) image from the set + cp "${TEMP_DIR}/${BASE}-0.png" "${DEST}" +else + cp "${TEMP_DIR}/${BASE}.png" "${DEST}" +fi + +rm -rf "${TEMP_DIR}" + +trap - EXIT +cleanUp diff --git a/bin/pngToIcns b/bin/pngToIcns index 4d05787..655925a 100755 --- a/bin/pngToIcns +++ b/bin/pngToIcns @@ -1,13 +1,17 @@ #!/bin/sh -# modified from https://github.com/daveish/png2icns for OSX 10.11 - -# USAGE +### USAGE # ./pngToIcns # Example # ./pngToIcns.sh ~/sample.png ~/Desktop/converted.icns +# exit the shell script on error immediately +set -e + +# import script as variable +CONVERT_TO_PNG="${BASH_SOURCE%/*}/convertToPng" + # Exec Paths ICONUTIL=$(which iconutil) IMAGEMAGICK_CONVERT=$(which convert) @@ -41,63 +45,40 @@ if [ -z "${DEST}" ]; then exit 1 fi - # File Infrastructure NAME=$(basename "${SOURCE}") EXT="${NAME##*.}" BASE="${NAME%.*}" +TEMP_DIR="temp_icon" ICONSET="${BASE}.iconset" -mkdir "${ICONSET}" +function cleanUp() { + rm -rf "${TEMP_DIR}" + rm -rf "${ICONSET}" +} -# check if .ico is a sequence -IS_ICO_SET="$(identify ${SOURCE} | grep -e "\w\.ico\[0")" +trap cleanUp EXIT -if [ "${IS_ICO_SET}" ]; then - # extract the largest(?) image from the set - convert "${SOURCE}" "${ICONSET}/${BASE}.png" - SOURCE="${ICONSET}/${BASE}-0.png" -fi +mkdir -p "${TEMP_DIR}" +mkdir -p "${ICONSET}" -# Debug Info -# echo "SOURCE: ${SOURCE}" -# echo "NAME: $NAME" -# echo "BASE: $BASE" -# echo "EXT: $EXT" -# echo "ICONSET: $ICONSET" - -# Get source image info -#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" -# -#if [ "x${SRCFORMAT}" != "xpng" ]; then -# echo "ERR: Source image format should be png." >&2 -# exit 1; -#fi +PNG_PATH="${TEMP_DIR}/icon.png" +${CONVERT_TO_PNG} "${SOURCE}" "${PNG_PATH}" # 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" - +convert "${PNG_PATH}" -define png:big-depth=16 -define png:color-type=6 -sample 16x16 "${ICONSET}/icon_16x16.png" +convert "${PNG_PATH}" -define png:big-depth=16 -define png:color-type=6 -sample 32x32 "${ICONSET}/icon_16x16@2x.png" +convert "${PNG_PATH}" -define png:big-depth=16 -define png:color-type=6 -sample 32x32 "${ICONSET}/icon_32x32.png" +convert "${PNG_PATH}" -define png:big-depth=16 -define png:color-type=6 -sample 64x64 "${ICONSET}/icon_32x32@2x.png" +convert "${PNG_PATH}" -define png:big-depth=16 -define png:color-type=6 -sample 128x128 "${ICONSET}/icon_128x128.png" +convert "${PNG_PATH}" -define png:big-depth=16 -define png:color-type=6 -sample 256x256 "${ICONSET}/icon_128x128@2x.png" +convert "${PNG_PATH}" -define png:big-depth=16 -define png:color-type=6 -sample 256x256 "${ICONSET}/icon_256x256.png" +convert "${PNG_PATH}" -define png:big-depth=16 -define png:color-type=6 -sample 512x512 "${ICONSET}/icon_256x256@2x.png" +convert "${PNG_PATH}" -define png:big-depth=16 -define png:color-type=6 -sample 512x512 "${ICONSET}/icon_512x512.png" +convert "${PNG_PATH}" -define png:big-depth=16 -define png:color-type=6 -sample 1024x1024 "${ICONSET}/icon_512x512@2x.png" # Create an icns file lefrom the iconset iconutil -c icns "${ICONSET}" -o "${DEST}" -# Clean up the iconset -rm -rf "${ICONSET}" - +trap - EXIT +cleanUp