Same as @Henry (comment above) but takes as argument the PNG filename and outputs the ICNS with the same name.
NOTE : The PNG file name is only expected to have 1 point to separate extension, i.e. xpto.png .
(updated for shell script)
So, save the code below to a filed called "CreateICNS.sh" in the folder where your png file is, and give it execution permisisons.
Code:
#!/bin/bash
IFS='.' read -ra ADDR <<< "$1"
ICONSET=${ADDR[0]}.iconset
mkdir $ICONSET
sips -z 16 16 $1 --out $ICONSET/icon_16x16.png
sips -z 32 32 $1 --out $ICONSET/icon_16x16@2x.png
sips -z 32 32 $1 --out $ICONSET/icon_32x32.png
sips -z 64 64 $1 --out $ICONSET/icon_32x32@2x.png
sips -z 128 128 $1 --out $ICONSET/icon_128x128.png
sips -z 256 256 $1 --out $ICONSET/icon_128x128@2x.png
sips -z 256 256 $1 --out $ICONSET/icon_256x256.png
sips -z 512 512 $1 --out $ICONSET/icon_256x256@2x.png
sips -z 512 512 $1 --out $ICONSET/icon_512x512.png
cp $1 $ICONSET/icon_512x512@2x.png
iconutil -c icns $ICONSET
rm -R $ICONSET
HOW TO USE :
Then in the terminal, "cd" to the same folder and type :
./CreateICNS.sh {PNG filename}
where {PNG filename} is the name of your PNG file, i.e. xpto.png .
If your file would be named abc.png you would use :
./CreateICNS.sh abc.png
UPDATE 2021-05-20 :
I have an updated version of this but I'm not sure where I found it to leave here the correct reference so if someone is the owner of this or knows a link on some internet page, please comment so I can update credits :
This is a complete bash script, so you should save it as for example png2icns.sh
and give it execution permissions.
Then you can call png2icns.sh pngfile1.png
and it will generate the ICNS file as well as a iconset folder containing all icon resolutions between 16x16 and 512x512 .
#!/bin/bash
# Creates an icns file from a source image
src_image="$1"
if [ -z "$1" ]; then
echo "No source image was passed to this script"
exit 1
fi
icns_name="$2"
if [ -z "$2" ]; then
icns_name="iconbuilder"
fi
if [ "${src_image:(-3)}" != "png" ]; then
echo "Source image is not a PNG, making a converted copy..."
/usr/bin/sips -s format png "$src_image" --out "${src_image}.png"
if [ $? -ne 0 ]; then
echo "The source image could not be converted to PNG format."
exit 1
fi
src_image="${src_image}.png"
fi
iconset_path="./${icns_name}.iconset"
if [ -e "$iconset_path" ]; then
/bin/rm -r "$iconset_path"
if [ $? -ne 0 ]; then
echo "There is a pre-existing file/dir $iconset_path the could not be deleted"
exit 1
fi
fi
/bin/mkdir "$iconset_path"
icon_file_list=(
"icon_16x16.png"
"icon_16x16@2x.png"
"icon_32x32.png"
"icon_32x32@2x.png"
"icon_128x128.png"
"icon_128x128@2x.png"
"icon_256x256.png"
"icon_256x256@2x.png"
"icon_512x512.png"
"icon_512x512@2x.png"
)
icon_size=(
'16'
'32'
'32'
'64'
'128'
'256'
'256'
'512'
'512'
'1024'
)
counter=0
for a in ${icon_file_list[@]}; do
icon="${iconset_path}/${a}"
/bin/cp "$src_image" "$icon"
icon_size=${icon_size[$counter]}
/usr/bin/sips -z $icon_size $icon_size "$icon"
counter=$(($counter + 1))
done
echo "Creating .icns file from $iconset_path"
/usr/bin/iconutil -c icns "$iconset_path"
if [ $? -ne 0 ]; then
echo "There was an error creating the .icns file"
exit 1
fi
echo "Done"
exit 0