-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpng2icns.sh
executable file
·189 lines (155 loc) · 4.55 KB
/
png2icns.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
#
# Ce script transforme un unique PNG de 1024x1024 en un fichier ICNS
#
# query="/Users/philippeperret/Programmation/Electron/afaire/_DEV_/imagerie/Chantier/Logo/Logo_01.png"
# ---------------------------------------------------------------------
# Arguments parsing
# Options par défaut
verbose=false
keepiconsetfolder=false
for arg in "$@"
do
case $arg in
-h|--help)
echo "
png2icns
let's convert a 1024x1024 PNG image to a ICNS icon (for your Mac).
PNG -> ICNS
Usage
-----
./png2icns [options] /path/to/image.png
Options
-------
-h --help Display this help
-v --verbose Verbose mode (default: false)
-d --keep-iconset-folder
"
exit 0
shift # past argument
;;
-v|--verbose)
verbose=true
shift
;;
-d|--keep-iconset-folder)
keepiconsetfolder=true
shift
;;
*)
query=$arg
;;
esac
shift
done
readonly VERBOSE=$verbose
readonly KEEP_ICONSET_FOLDER=$keepiconsetfolder
readonly FILE="$query"
FNAME=$(basename $FILE)
FILE_FOLDER=$(dirname $FILE)
AFFIXE=${FNAME%%.*}
ICON_NAME="${AFFIXE}.icns"
$VERBOSE && echo -e "\n\n=== PNG to ICNS Conversion ($FNAME -> $ICON_NAME) ===\n"
# ---------------------------------------------------------------------
# Tests
# File must exist
if [ ! -f $FILE ];
then
echo "ERROR: File '$FILE' doesn't exist."
exit 1
fi
# File must have .png extension
FILE_EXTNAME=${FILE##*.}
if [ ! $FILE_EXTNAME = 'png' ];
then
echo "ERROR: File '$FILE' must have a '.png' extname not a '.$FILE_EXTNAME' extname."
exit 1
fi
# File should have 1024x1024 sixe
retour=`sips -g pixelWidth -g pixelHeight "$FILE"`
extrait=${retour#*pixelWidth: }
WIDTH=$((0+${extrait%% *}))
extrait=${retour#*pixelHeight: }
HEIGHT=$((0+${extrait%% *}))
$VERBOSE && echo "Original image size: ${WIDTH}x${HEIGHT}"
if (($WIDTH < 1024)) ;
then
echo "WARNING: PNG Image width (${WIDTH}px) is less then 1024 pixels. Perhaps the icon'll be ugly…"
fi
if (($HEIGHT < 1024));
then
echo "WARNING: PNG Image height (${HEIGHT}px) is less then 1024 pixels. Perhaps the icon'll be ugly…"
fi
if (( $HEIGHT != $WIDTH ));
then
echo "WARNING: PNG Image width and height are not the same (${WIDTH}px vs ${HEIGHT}px). Perhaps the icon'll be ugly…"
fi
# ---------------------------------------------------------------------
$VERBOSE && echo "Fichier original: ${FNAME}"
$VERBOSE && echo "Affixe : ${AFFIXE}"
# The iconset folder
# We put inside all the png we need to build the icns icon
ICONSET_FOLDER="./Icones.iconset"
# Go in the image folder
cd $FILE_FOLDER
$VERBOSE && echo `pwd`
# = Si le dossier .iconset existe, il faut le détruire avant
# = de le faire
if [ -d $ICONSET_FOLDER ];
then
rm -R $ICONSET_FOLDER
fi
mkdir $ICONSET_FOLDER
# Si l'icon existe déjà, il faut la détruire
if [ -f "./${ICON_NAME}" ];
then
rm ./$ICON_NAME
$VERBOSE && echo "A icon $ICON_NAME already existed. I removed it."
fi
# = D'abord, on doit faire un set de PNG à l'aide de sips
for width in 512 256 128 32 16 ;
do
fname_dest="icon_${width}x${width}.png"
$VERBOSE && printf "Création du PNG ${fname_dest}… "
# On fait une copie du fichier redimensionné dans le dossier
sips $FNAME -z $width $width --out "${ICONSET_FOLDER}/${fname_dest}"
$VERBOSE && echo "done"
# On fait des fichier @2x pour tout les fichiers hors 1024 et 516
# sips -z 32 32 Icon1024.png --out MyIcon.iconset/icon_16x16@2x.png
dblwidth=$(( 2 * $width ))
fname_dest_dblw="icon_${width}x${width}@2x.png"
$VERBOSE && printf "Fichier ${fname_dest_dblw}… "
sips $FNAME -z $dblwidth $dblwidth --out "${ICONSET_FOLDER}/${fname_dest_dblw}"
$VERBOSE && echo "done"
done
# = Maintenant que le set de fichiers PNG est fabriqué, on peut construire
# = les ICNS
# cp Icon1024.png MyIcon.iconset/icon_512x512@2x.png
# === Faire les icns ===
$VERBOSE && printf "Generating ICNS…"
iconutil -c icns "$ICONSET_FOLDER"
if [ ! -f icones.icns ];
then
echo "ERROR: Unable to built ICNS icon… Sorry."
exit 1
fi
# === On renomme le fichier obtenu, qui s'appelle pour le moment
# === icone.icns
mv icones.icns "$ICON_NAME"
$VERBOSE && echo " done"
# === Détruire le dossier des PNG ===
if $KEEP_ICONSET_FOLDER ;
then
$VERBOSE && echo "I don't remove iconset folder with all PNG images."
else
$VERBOSE && printf "* Removing iconset folder…"
rm -R "$ICONSET_FOLDER"
$VERBOSE && echo " done"
fi
# Si l'icone existe bien, c'est un succès
if [ -f "./${ICON_NAME}" ];
then
echo -e "\nIcon '${ICON_NAME}' generated with success.\nIn: ${FILE_FOLDER}/"
else
echo "WARNING: Strange… Can't find the ./${ICON_NAME} icon…"
fi