Skip to content

Instantly share code, notes, and snippets.

@pd95
Last active December 28, 2022 20:56
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save pd95/f8afdeb6139d96019bd60b11a70d6367 to your computer and use it in GitHub Desktop.
Save pd95/f8afdeb6139d96019bd60b11a70d6367 to your computer and use it in GitHub Desktop.
A ZSH command file to produce the various PNG files required for iOS and macOS app Icons using `sips` (scriptable image processing system) available on any macOS. The resulting PNGs in the "AppIcons" folder can then be dragged into Xcode's `Assets.xcassets` onto a placeholder for an AppIcon.
#!/bin/zsh
# Shell Script to create all relevant icons from an high resolution artwork
if [ "x$1" != "x" -a -f "$1" ] ; then
INPUT=$1
else
INPUT="Artwork.png"
fi
if [ ! -f "$INPUT" ]; then
echo "Input file not specified"
exit 1
fi
mkdir -p AppIcons
while IFS= read -r line ; do
FILENAME=${line% *}
SIZE=${line#* }
if [ "x$FILENAME" != "x" ] ; then
COMMAND="sips -s format png \"$INPUT\" --resampleHeightWidth $SIZE $SIZE --out \"AppIcons/$FILENAME\""
echo "$COMMAND"
eval "$COMMAND"
else
echo ""
fi
done <<EOF
AppIcon~iPhone-20@2x.png 40
AppIcon~iPhone-20@3x.png 60
AppIcon~iPhone-29@1x.png 29
AppIcon~iPhone-29@2x.png 58
AppIcon~iPhone-29@3x.png 87
AppIcon~iPhone-40@2x.png 80
AppIcon~iPhone-40@3x.png 120
AppIcon~iPhone-57@1x.png 57
AppIcon~iPhone-57@2x.png 114
AppIcon~iPhone-60@2x.png 120
AppIcon~iPhone-60@3x.png 180
AppIcon~iPad-20@1x.png 20
AppIcon~iPad-20@2x.png 40
AppIcon~iPad-29@1x.png 29
AppIcon~iPad-29@2x.png 58
AppIcon~iPad-40@1x.png 40
AppIcon~iPad-40@2x.png 80
AppIcon~iPad-76@1x.png 76
AppIcon~iPad-76@2x.png 152
AppIcon~iPad-83.5@2x.png 167
AppIcon~iOS-Marketing-1024@1x.png 1024
AppIcon~macOS-16@1x.png 16
AppIcon~macOS-16@2x.png 32
AppIcon~macOS-32@1x.png 32
AppIcon~macOS-32@2x.png 64
AppIcon~macOS-128@1x.png 128
AppIcon~macOS-128@2x.png 256
AppIcon~macOS-256@1x.png 256
AppIcon~macOS-256@2x.png 512
AppIcon~macOS-512@1x.png 512
AppIcon~macOS-512@2x.png 1024
EOF
@pd95
Copy link
Author

pd95 commented Mar 11, 2020

This script basically converts the input file (passed as an argument or "Artwork.png" in the current directory) and produces all relevant AppIcon files in the AppIcons directory.

sips -s format png "Artwork.png" --resampleHeightWidth 40 40 --out "AppIcons/AppIcon~iPhone-20@2x.png"
sips -s format png "Artwork.png" --resampleHeightWidth 60 60 --out "AppIcons/AppIcon~iPhone-20@3x.png"
sips -s format png "Artwork.png" --resampleHeightWidth 58 58 --out "AppIcons/AppIcon~iPhone-29@2x.png"
sips -s format png "Artwork.png" --resampleHeightWidth 87 87 --out "AppIcons/AppIcon~iPhone-29@3x.png"
sips -s format png "Artwork.png" --resampleHeightWidth 80 80 --out "AppIcons/AppIcon~iPhone-40@2x.png"
sips -s format png "Artwork.png" --resampleHeightWidth 120 120 --out "AppIcons/AppIcon~iPhone-40@3x.png"
sips -s format png "Artwork.png" --resampleHeightWidth 120 120 --out "AppIcons/AppIcon~iPhone-60@2x.png"
sips -s format png "Artwork.png" --resampleHeightWidth 180 180 --out "AppIcons/AppIcon~iPhone-60@3x.png"

sips -s format png "Artwork.png" --resampleHeightWidth 20 20 --out "AppIcons/AppIcon~iPad-20@1x.png"
sips -s format png "Artwork.png" --resampleHeightWidth 40 40 --out "AppIcons/AppIcon~iPad-20@2x.png"
sips -s format png "Artwork.png" --resampleHeightWidth 29 29 --out "AppIcons/AppIcon~iPad-29@1x.png"
sips -s format png "Artwork.png" --resampleHeightWidth 58 58 --out "AppIcons/AppIcon~iPad-29@2x.png"
sips -s format png "Artwork.png" --resampleHeightWidth 40 40 --out "AppIcons/AppIcon~iPad-40@1x.png"
sips -s format png "Artwork.png" --resampleHeightWidth 80 80 --out "AppIcons/AppIcon~iPad-40@2x.png"
sips -s format png "Artwork.png" --resampleHeightWidth 76 76 --out "AppIcons/AppIcon~iPad-76@1x.png"
sips -s format png "Artwork.png" --resampleHeightWidth 152 152 --out "AppIcons/AppIcon~iPad-76@2x.png"
sips -s format png "Artwork.png" --resampleHeightWidth 167 167 --out "AppIcons/AppIcon~iPad-83.5@2x.png"

sips -s format png "Artwork.png" --resampleHeightWidth 1024 1024 --out "AppIcons/AppIcon~iOS-Marketing-1024@1x.png"

@pd95
Copy link
Author

pd95 commented May 25, 2020

Thanks to pilgrim-ivanhoefor sharing his knowledge about the Xcode naming conventions on stack-overflow "What is the correct App Icon (appicon) naming convention for Xcode 9.2?". Now it is finally possible to drag all the icons to Xcodes AppIcon placeholder and they are automatically assigned to the correct placeholder.
AppIcon-Screencapture

@KevinQuisquater
Copy link

KevinQuisquater commented Nov 3, 2020

Thank you so much! I have been looking for such an easy solution for years.

To save time to complete beginners like me: to run this, open Terminal, type sh , drag the .command file into the Terminal (it will paste its path), then drag your .png icon into the Terminal to and tap Enter.

The final command will look like this:

sh {PATH-TO-AppIcons.command} {PATH_TO_YOUR_PNG}

Also, in the file itself, I had to delete the ; at the end of line 16 for the script to run.

There are 3 formats that are asked by Xcode for iPhones that are not here, you can add them to the file too:

AppIcon~iPhone-29@1x.png 29
AppIcon~iPhone-57@1x.png 57
AppIcon~iPhone-57@2x.png 114

Here is the final script without the ; and with all the images formats:

#!/bin/zsh
# Shell Script to create all relevant icons from an high resolution artwork

if [ "x$1" != "x" -a -f "$1" ] ; then
    INPUT=$1
else 
    INPUT="Artwork.png"
fi

if [ ! -f "$INPUT" ]; then
    echo "Input file not specified"
    exit 1
fi
mkdir -p AppIcons

while IFS= read -r line ; do
  FILENAME=${line% *}
  SIZE=${line#* }
  if [ "x$FILENAME" != "x" ] ; then
    COMMAND="sips -s format png \"$INPUT\" --resampleHeightWidth $SIZE $SIZE --out \"AppIcons/$FILENAME\""
    echo "$COMMAND"
    eval "$COMMAND"
  else
    echo ""
  fi
done <<EOF
AppIcon~iPhone-20@2x.png 40
AppIcon~iPhone-20@3x.png 60
AppIcon~iPhone-29@1x.png 29
AppIcon~iPhone-29@2x.png 58
AppIcon~iPhone-29@3x.png 87
AppIcon~iPhone-40@2x.png 80
AppIcon~iPhone-40@3x.png 120
AppIcon~iPhone-57@1x.png 57
AppIcon~iPhone-57@2x.png 114
AppIcon~iPhone-60@2x.png 120
AppIcon~iPhone-60@3x.png 180

AppIcon~iPad-20@1x.png 20
AppIcon~iPad-20@2x.png 40
AppIcon~iPad-29@1x.png 29
AppIcon~iPad-29@2x.png 58
AppIcon~iPad-40@1x.png 40
AppIcon~iPad-40@2x.png 80
AppIcon~iPad-76@1x.png 76
AppIcon~iPad-76@2x.png 152
AppIcon~iPad-83.5@2x.png 167

AppIcon~iOS-Marketing-1024@1x.png 1024
EOF

@pd95
Copy link
Author

pd95 commented Nov 4, 2020

Thanks @KevinQuisquater! I've updated my script accordingly, to ensure everybody gets the latest version.

It seems that zsh does not complain about the unnecessary ; I had added. The missing icons you've identified are probably due to my focus on iOS 13 and above. Probably older iOS releases/devices expect even more icon variants...

@ice-cream-coder
Copy link

Thanks for the useful script. I think it's worth noting that when I used the script some of my icons where slightly of center. Resampling is quick approach, but I think I'm going to get better result rendering each on individually for the original art file.
Screen Shot 2021-01-22 at 8 06 19 AM

@pd95
Copy link
Author

pd95 commented Jan 22, 2021

Hi Roy

The result of the script depends on the quality of the input. Either your input image was not square (width = height) or the text was not aligned.
I tried to reproduce the problem myself and got an acceptable quality (see below). None of the icons seems to be misaligned.

App Icons

I created these icons based on an artwork image with dimensions 1024x1024 and the text properly centered.

Best regards
Philipp

@ice-cream-coder
Copy link

Ah, I thought the pixels where shifting from he resampling algorithm used in your script. I checked my 1024pt icon that I ran the script on and the text is actually off center. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment