Skip to content

Instantly share code, notes, and snippets.

@sag333ar
Last active July 26, 2022 12:27
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 sag333ar/bf55ac27c6ffa5fd2ee72fd4f5b79fe9 to your computer and use it in GitHub Desktop.
Save sag333ar/bf55ac27c6ffa5fd2ee72fd4f5b79fe9 to your computer and use it in GitHub Desktop.
Generate 1x 2x 3x images from supplied 3x assets. Following shell-script can be used for iOS Asset catalogue.
for f in *.png
do
# Process to get File Name & 2x, 3x file names
xNAME=`echo "$f" | cut -d'.' -f1`
cp "$f" "$xNAME@3x.png"
cp "$f" "$xNAME@2x.png"
# Set proper resolution to original file
sips -s dpiHeight 72.0 -s dpiWidth 72.0 "$f"
# Get Width of original file
xWIDTH=`sips -g pixelWidth "$f" | cut -d':' -f 2 | tail -1 | cut -d' ' -f 2`
# Get Height of original file
xHEIGHT=`sips -g pixelHeight "$f" | cut -d':' -f 2 | tail -1 | cut -d' ' -f 2`
# Variables for 1x
xWIDTH1=`expr $xWIDTH / 3`
xHEIGHT1=`expr $xHEIGHT / 3`
# Variables for 2x
xWIDTH2=`expr $xWIDTH1 \* 2`
xHEIGHT2=`expr $xHEIGHT1 \* 2`
# Applying size to all three images
sips -z "$xHEIGHT1" "$xWIDTH1" "$f"
sips -z "$xHEIGHT2" "$xWIDTH2" "$xNAME@2x.png"
# Creating Asset folder & moving images into asset folder
mkdir "$xNAME.imageset"
mv "$f" "$xNAME.imageset/"
mv "$xNAME@2x.png" "$xNAME.imageset/"
mv "$xNAME@3x.png" "$xNAME.imageset/"
echo "{ \"images\" : [ { \"idiom\" : \"universal\", \"filename\" : \"$f\", \"scale\" : \"1x\" }, { \"idiom\" : \"universal\", \"filename\" : \"$xNAME@2x.png\", \"scale\" : \"2x\" }, { \"idiom\" : \"universal\", \"filename\" : \"$xNAME@3x.png\", \"scale\" : \"3x\" } ], \"info\" : { \"version\" : 1, \"author\" : \"xcode\" } }" > Contents.json
mv Contents.json "$xNAME.imageset/"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment