-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjpgresize.sh
executable file
·136 lines (119 loc) · 3.04 KB
/
jpgresize.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
param="$1"
cliext="${1##*.}"
# settings
if [[ -z $2 ]]; then
longestSide=2048
else
longestSide=$2
fi
size=$longestSide'x'$longestSide'>'
colorspaceForResize='-colorspace LAB'
optional1='-set option:filter:lobes 8'
sigmaContrast1='+sigmoidal-contrast 6.5,50%'
sigmaContrast2='-sigmoidal-contrast 6.5,50%'
resizeType='-filter Lanczos'
sharper='-unsharp 1.5x1+0.7+0.02'
sharper=''
# Upscaling "-unsharp 0x0.75+0.75+0.008"
# Downsampling "-unsharp 1.5x1+0.7+0.02".
quality='-quality 100'
if [[ $2 == 0 ]]; then
resizeOperator=''
sigmaContrast1=''
sigmaContrast2=''
resizeType=''
longestSide='orig'
else
resizeOperator='-distort resize '"$size"
fi
colorspaceAfterResize='-colorspace sRGB'
jpegQuality=90
cliArray=("$colorspaceForResize" "$optional1" "$sigmaContrast1" "$resizeType" "$resizeOperator" "$colorspaceAfterResize" "$sharper" "$sigmaContrast2" "$quality")
pngQuality=90
# cli utilites
joPath="$(which jpegoptim)"
if [[ -z $joPath ]]; then
echo jpegoptim is not installed
brew install jpegoptim
fi
pqPath="$(which pngquant)"
if [[ -z $pqPath ]]; then
echo pngquant is not installed
brew install pngquant
fi
resizeJpg() {
filename="$1"
DIR=$(dirname "${filename}")
FN=$(basename "${filename}")
outDir="$DIR"/"$longestSide"'__q'"$jpegQuality"
if ! [[ -d "$outDir" ]]; then
mkdir "$outDir"
fi
outFn="$outDir"/"${FN%.*}"
convert -quiet "$filename"[0] ${cliArray[@]} "$outFn"'.jpg'
jpegoptim -v -T 10 -m $jpegQuality -p --strip-none "$outFn"'.jpg'
};
export -f resizeJpg;
resizeTiff() {
filename="$1"
DIR=$(dirname "${filename}")
FN=$(basename "${filename}")
outDir="$DIR"/"$longestSide"'__q'"$jpegQuality"
if ! [[ -d "$outDir" ]]; then
mkdir "$outDir"
fi
outFn="$outDir"/"${FN%.*}"
convert -quiet "$filename"[0] ${cliArray[@]} "$outFn"'.jpg'
jpegoptim -v -T 10 -m $jpegQuality -p --strip-none "$outFn"'.jpg'
};
export -f resizeTiff;
resizePng() {
filename="$1"
DIR=$(dirname "${filename}")
FN=$(basename "${filename}")
outDir="$DIR"/"$longestSide"'__q'"$jpegQuality"
if ! [[ -d "$outDir" ]]; then
mkdir "$outDir"
fi
outFn="$outDir"/"${FN%.*}"
convert "$filename" ${cliArray[@]} "$outFn"'.png'
pngquant --force --skip-if-larger --quality $pngQuality --speed 1 --nofs "$outFn"'.png'
};
export -f resizePng;
if [ -z "$param" ]
then
echo nothing to do
elif [ -d "$param" ]
then
echo 'directory '"$param"
jpegs=$(find "$param" -maxdepth 1 -iname '*.jpeg' -or -iname '*.jpg' -or -iname '*.tif' -or -iname '*.tiff')
pngs=$(find "$param" -maxdepth 1 -iname '*.png')
while read -r line; do
resizeJpg "$line"
done <<< "$jpegs"
while read -r line; do
resizePng "$line"
done <<< "$pngs"
# -exec bash -c 'resizeJpg "$0"' {} \;
elif [ -f "$param" ]
then
# echo 'file '$param
if [ $cliext == 'jpeg' ]
then
resizeJpg "$param"
elif [ $cliext == 'jpg' ]
then
resizeJpg "$param"
elif [ $cliext == 'tif' ]
then
resizeTiff "$param"
elif [ $cliext == 'tiff' ]
then
resizeTiff "$param"
elif [ $cliext == 'png' ]
then
resizePng "$param"
fi
else
echo 'wtf'
fi