-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsk-png.sh
executable file
·228 lines (155 loc) · 6.35 KB
/
jsk-png.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env bash
# ====================================================== project information ===
# author : jimmy mg lim ([email protected])
# source : https://github.com/mirageglobe/swissknife
# version : 0.1.0
# --------------------------------------------------------------- references ---
# - http://superuser.com/questions/134679/command-line-application-for-converting-svg-to-png-on-mac-os-x
# --------------------------------------------------------------------- main ---
# use pngquant for web (lossy)
# use optipng for print (lossless)
# use http://phantomjs.org/screen-capture.html to print to pdf or png from svg
# use convert -d 300 foo.pdf bar.png for higher ppi(dpi) - http://askubuntu.com/questions/50170/how-to-convert-pdf-to-image
# use ls *.jpg|while read i;do gm convert $i -resize "900x" -unsharp 2x0.5+0.5+0 -quality 98 `basename $i .jpg`_s.jpg;done
# to convert for android .... try gm convert "input.png" -resize 2481x3507 -bit-depth=32 "output.png"
# ----- include libraries
# this code converts a png for web to 800x600 WxH at 72 dpi (if needed)
# ----- Check Arguments
DTTITLE=""
DTTEXT=" "
EXPECTED_ARGS=2
E_BADARGS=65
if [ "$#" -lt "$EXPECTED_ARGS" ]; then
echo "$DTTEXT"
echo "$DTTITLE script:"
echo "$DTTEXT this script converts with graphicsmagick and does postcompression. Dependancies: graphicsmagick(brew), pngquant(brew) or pngquant-bin(npm), optipng(brew) or optipng-bin(npm), svgexport(npm)"
echo "$DTTEXT"
echo "$DTTITLE usage:"
echo "$DTTEXT $0 [options] [source:path/to/file] [target:path/to/file]"
echo "$DTTEXT $0 -g /path/to/image : convert to grayscale"
echo "$DTTEXT $0 -w /path/to/image : convert for (w)eb 800x600 WxH 72dpi png (webstandard)"
echo "$DTTEXT $0 -p /path/to/image : convert for (p)rint 2481x3507 WxH (fullA4) 300dpi png (printstandard)"
echo "$DTTEXT $0 -l /path/to/image : compress to (l)ossy based on pngquant"
echo "$DTTEXT $0 -s /path/to/image : compress to lossles(s) based on optipng"
echo "$DTTEXT $0 -i /path/to/image : (soon) generate icons including .ico .png appletouch win8tile"
echo "$DTTEXT $0 -o /path/to/image /new/file : (soon) set output file"
echo "$DTTEXT"
echo "$DTTITLE examples:"
echo "$DTTEXT $0 -w /home/useraccount/myfirst/image.png"
echo "$DTTEXT $0 -wl /home/useraccount/myfirst/image.png"
echo "$DTTEXT $0 -ws /home/useraccount/myfirst/image.svg"
echo "$DTTEXT"
echo "$DTTITLE notes:"
echo "$DTTEXT options l and s are not interoperable"
echo "$DTTEXT options w and p are not interoperable"
echo "$DTTEXT"
exit $E_BADARGS
fi
# ----- Define Variables
GMOPTIONS=$1
IMAGEFULL=$2
IMAGEEXT="${IMAGEFULL##*.}"
IMAGENAME="${IMAGEFULL%.*}"
IMAGEPNG="${IMAGENAME}.png"
DSTAMP=$(date +%y%m%d)
# ----- Main Code
#if target file is empty. set it to current file
echo "*** ----- "
echo "*** Running : MakeGMpng"
echo "*** ----- "
# checks first char of string to be -
echo "$DTTITLE validating options"
if [[ ${GMOPTIONS::1} == "-" ]]; then
echo "$DTTEXT [ok]"
else
echo "$DTTEXT options wrongly defined [abort]"
exit 1;
fi
# checking destination of file
echo "$DTTEXT checking graphics file ${IMAGEFULL}"
if [ ! -f "${IMAGEFULL}" ]; then
echo "$DTTEXT file not found [abort]"
exit 1
else
echo "$DTTEXT [ok]"
fi
# auto convert svg to 2x
if [[ "${IMAGEEXT}" == "svg" ]]; then
echo "$DTTEXT checking svgexport installation"
command -v gm >/dev/null 2>&1 || { echo "$ESST svgexport not installed [abort]" >&2; exit 1; }
echo "$DTTEXT [ok]"
echo "$DTTEXT export svg to png using svgexport"
svgexport ${IMAGEFULL} ${IMAGEPNG} png 100% "" 2x
echo "$DTTEXT [ok]"
fi
# backup current image
if [ -f "${IMAGEPNG}" ]; then
echo "$DTTEXT backing up current image"
cp -i "${IMAGEPNG}" "${IMAGENAME}_$DSTAMP.png"
echo "$DTTEXT [ok]"
fi
# convert grayscale
if [[ "${GMOPTIONS}" =~ "g" ]]; then
# checking application installation
echo "$DTTITLE convert to grayscale"
echo "$DTTEXT checking graphicsmagick installation"
command -v gm >/dev/null 2>&1 || { echo "$ESST graphicsmagick not installed [abort]" >&2; exit 1; }
echo "$DTTEXT [ok]"
# compressing image: may need to rescale to 300x300 dpi in option
echo "$DTTEXT grayify"
gm convert "${IMAGEPNG}" -type Grayscale -colorspace GRAY "${IMAGEPNG}"
echo "$DTTEXT [ok]"
fi
# convert for web 800x600 72dpi
if [[ "${GMOPTIONS}" =~ "w" ]]; then
# checking application installation
echo "$DTTITLE convert and compress for (w)eb"
echo "$DTTEXT checking graphicsmagick installation"
command -v gm >/dev/null 2>&1 || { echo "$ESST graphicsmagick not installed [abort]" >&2; exit 1; }
echo "$DTTEXT [ok]"
# compressing image: may need to rescale to 300x300 dpi in option
echo "$DTTEXT resizing image"
gm convert "${IMAGEPNG}" -resize 800x600 -quality 95 "${IMAGEPNG}"
echo "$DTTEXT [ok]"
fi
# convert for print 2481x3507 300dpi WxH A4portrait
if [[ "${GMOPTIONS}" =~ "p" ]]; then
# checking application installation
echo "$DTTITLE convert and compress for (p)rint"
echo "$DTTEXT checking graphicsmagick installation"
command -v gm >/dev/null 2>&1 || { echo "$ESST graphicsmagick not installed [abort]" >&2; exit 1; }
echo "$DTTEXT [ok]"
# compressing image: may need to rescale to 300x300 dpi in option
echo "$DTTEXT resizing image"
gm convert "${IMAGEPNG}" -resize 2481x3507 -quality 95 "${IMAGEPNG}"
echo "$DTTEXT [ok]"
fi
# lossy compression
if [[ "${GMOPTIONS}" =~ "l" ]]; then
echo "$DTTEXT checking pngquant installation"
command -v pngquant >/dev/null 2>&1 || { echo "$ESST pngquant not installed [abort]" >&2; exit 1; }
echo "$DTTEXT [ok]"
echo "$DTTITLE compress using pngquant (lossy)"
pngquant --force "${IMAGEPNG}"
echo "$DTTEXT [ok]"
echo "$DTTEXT replacing output png with compressed version"
mv "${IMAGENAME}-fs8.png" "${IMAGEPNG}"
echo "$DTTEXT [ok]"
fi
# lossless compression
if [[ "${GMOPTIONS}" =~ "s" ]]; then
echo "$DTTEXT checking optipng installation"
command -v optipng >/dev/null 2>&1 || { echo "$ESST optipng not installed [abort]" >&2; exit 1; }
echo "$DTTEXT [ok]"
echo "$DTTITLE compress using optipng (lossy)"
optipng "${IMAGEPNG}"
echo "$DTTEXT [ok]"
fi
if [[ "${GMOPTIONS}" =~ "i" ]]; then
echo "$DTTITLE converting tbc"
fi
if [[ "${GMOPTIONS}" =~ "o" ]]; then
echo "$DTTITLE converting tbc"
fi
# complete
echo "$DTTITLE done ... [ok]"