-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreaderDownSizer.sh
155 lines (121 loc) · 4.19 KB
/
readerDownSizer.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
#!/bin/bash
# Requirements: Linux bash/ImageMagick
# 17 1024X768 in 8 secs with 100 quality, 30000 around 4 hours
# You can edit these settings:
# Default source
default_src="5.txt";
# Default destination
default_dst="2";
# Output JPG quality: maximum is 100 (recommended)
default_qua="100"
# Minimum width to resize greater than that
default_min_width="800"
function resize(){
echo "Conversion started..."
# Setup arguments/sharpen/unsharp/quality parameters
local argument1="-resize 320"
local argument2="-resize 600"
local unsharp="-unsharp 0.4x0.4+0.4+0.008"
local sharpen="-sharpen 0x2"
local quality="-quality ${qua}"
local COUNTER=0
local last_resized=""
# to make the script read white spaces
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
while read -r line
do
echo "Checking ${line}"
# create line with %20 instead of space
lineNoSpace=${line// /%20}
# # Create the same directory as it read for small
# if [[ ! -e ${dst}/small/${line} ]]; then
# mkdir ${dst}/small${line}
# fi
# # Create the same directory as it read for medium
# if [[ ! -e ${dst}/medium/${line} ]]; then
# mkdir ${dst}/medium${line}
# fi
# # Create the same directory as it read for errors
# if [[ ! -e ${dst}/error/${line} ]]; then
# mkdir ${dst}/error${line}
# fi
# Detect source image width and height
src_w=$(identify -format "%w" "${lineNoSpace}")
# Go to the next iteration if error
if [ $? != 0 ]; then
echo "${line} not accessible, moving to errorLog.txt"
echo $line >> $dst/errorLog.txt
continue
fi
src_h=$(identify -format "%h" "${lineNoSpace}")
# If the image considered large
if ((${src_w} > ${defWidth})); then
echo "Qualified to be resized, dimentions: ${src_w} X ${src_h}"
# Resize, sharpen
echo "processing ${line}"
# Creating the path
local temp=${line}
# Take the file name out
local trimedPath=${temp%/*}
# Take the http:// out
local path=${trimedPath#*/}
if [[ ! -e $dst${path} ]]; then
echo "Creating path $dst$path"
mkdir -p $dst${path}
fi
if [[ ! -e $dst${path}/small ]]; then
mkdir $dst${path}/small
fi
if [[ ! -e $dst${path}/medium ]]; then
mkdir $dst${path}/medium
fi
if [[ ! -e $dst${path}/error ]]; then
mkdir $dst${path}/error
fi
# Not using the arguments because it errors out when try to handle spaces in the entry name
convert ${lineNoSpace} -resize 320 ${dst}${path}/small/$(basename $line)
convert ${lineNoSpace} -resize 600 ${dst}${path}/medium/$(basename $line)
# If the last command result has error
if [ $? == 0 ]; then
COUNTER=$((COUNTER+1))
echo "Conversion successful"
echo ""
else
echo "Sending to error directory"
cp ${line} $dst${path}/error/$(basename $line)
fi
last_resized=${line}
fi
done < ${src}
# Set the IFS back to normal
IFS=$SAVEIFS
echo "Last resized image: ${last_resized}"
echo "Number of files resized: ${COUNTER}"
}
# Ask for source image, or use default value
echo "Welcome, add a line to the beginning and end of text file to make sure they won't be skipped!"
echo "Enter source path text file (.txt)/Enter to keep default (${default_src}): "
read src
src=${src:-${default_src}}
# Ask for destination path, or use default value
echo "Enter destination path/Enter to keep default (${default_dst}):"
read dst
dst=${dst:-${default_dst}}
# Ask for Minimum Width, or use default value
echo "Enter Minimum Width (resize images greater than this size)/Enter to keep default (${default_min_width}):"
read defWidth
defWidth=${defWidth:-${default_min_width}}
# Ask for quality, or use default value
echo "Enter quality/Enter to keep default (${default_qua}):"
read qua
qua=${qua:-${default_qua}}
# Create the destination path
if [[ ! -e $dst ]]; then
echo "Making ${dst} Directory..."
mkdir $dst
fi
# Call the resize function
resize
# Done!
echo "Done!"