-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash_image_resizer.sh
More file actions
131 lines (111 loc) · 4.56 KB
/
bash_image_resizer.sh
File metadata and controls
131 lines (111 loc) · 4.56 KB
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
#!/bin/bash
# =========================================================================
# The purpose of this script is to enable quick and effortless conversion
# of multiple images to the same size.
#
# Features:
# - uses imagemagick and mogrify for resizing
# - can either replace original file or write to a sibling directory
# - traverses sub-directories
# - supports multiple file formats
# - skips already processed files (toggle)
# =========================================================================
### OPTIONS
OVERWRITE_ORIGINAL_FILE=false
TARGET_RESOLUTION=1920
SOURCE_PIC_DIR='/mnt/d/temp/company_photos_dump2022.12'
DESTINATION_PIC_DIR='/mnt/d/temp/small/test1'
# space separated extensions, case-sensitive. ie "JPG jpg jpeg"
ELIGIBLE_PIC_EXTENSIONS='PNG JPG JPEG png jpg jpeg'
ELIGIBLE_PIC_EXTENSIONS_REGEX='PNG\|JPG\|JPEG\|png\|jpg\|jpeg' # MUST BE THE SAME AS ABOVE!
SHOW_UNPROCESSED=false
SKIP_EXISTING=true
### DO NOT CHANGE ANYTHING BELOW
TOTAL_FILES_PROCESSED=0
SKIPPED_FILES_COUNT=0
welcome_prompt() {
echo "Batch Image resizer v0.1"
echo "========================"
echo
echo "Attempting to resize all images found in $SOURCE_PIC_DIR and
it's sub-directories."
echo
echo "Only files matching the following extensions will be resized:"
for ext in $ELIGIBLE_PIC_EXTENSIONS
do
echo " *.$ext"
done
read -n 1 -r -s -p $'Press enter to continue...\n'
}
# DEFINITIONS
main() {
clear
cd $SOURCE_PIC_DIR
for ext in $ELIGIBLE_PIC_EXTENSIONS; do
OIFS="$IFS"
IFS=$'\n'
echo
echo ">> $ext"
ELIGIBLE_DIRS=`find . -type f -name "*.$ext" -exec dirname {} \; | sort -u`
for dir in $ELIGIBLE_DIRS; do
process_single_directory $ext "$dir"
done
IFS="$OIFS"
done
}
# $1 = extension, $2 = dir
process_single_directory() {
extension=$1
directory=$2
cd $directory
if [ "$OVERWRITE_ORIGINAL_FILE" = true ]; then
echo "mogrify -resize ${TARGET_RESOLUTION}x ./*.$extension"
mogrify -resize ${TARGET_RESOLUTION}x ./*.$extension
else
if [ "$SKIP_EXISTING" = true ]; then
if [ -d "$DESTINATION_PIC_DIR/$directory" ]; then
echo "Found an existing target directory $DESTINATION_PIC_DIR/$directory"
ALREADY_PROCESSED_COUNT=`find $DESTINATION_PIC_DIR/$directory -type f -name "*.$extension" -exec printf x \; | wc -c`
TO_BE_PROCESSED_COUNT=`find $SOURCE_PIC_DIR/$directory -type f -name "*.$extension" -exec printf x \; | wc -c`
echo "ALREADY_PROCESSED_COUNT $ALREADY_PROCESSED_COUNT : TO_BE_PROCESSED_COUNT $TO_BE_PROCESSED_COUNT"
if [ "$ALREADY_PROCESSED_COUNT" -eq "$TO_BE_PROCESSED_COUNT" ]; then
echo "Number of files to be processed is equal to number of existing files. Skipping directory."
SKIPPED_FILES_COUNT=$((SKIPPED_FILES_COUNT+TO_BE_PROCESSED_COUNT))
else
echo "Number of files to be processed is NOT equal to number of existing files. Reprocessing directory."
run_resize "$DESTINATION_PIC_DIR/$directory" "$extension"
fi
else
echo "SKIP_EXISTING is enabled but $DESTINATION_PIC_DIR/$directory does not exist. Proceeding with resize"
run_resize "$DESTINATION_PIC_DIR/$directory" "$extension"
fi
else
run_resize "$DESTINATION_PIC_DIR/$directory" "$extension"
fi
fi
cd $SOURCE_PIC_DIR
}
run_resize() {
DESTINATION_DIR=$1
EXTENSION=$2
echo "mogrify -resize ${TARGET_RESOLUTION}x -path '$DESTINATION_DIR' ./*.$EXTENSION"
mkdir -p "$DESTINATION_DIR"
mogrify -resize ${TARGET_RESOLUTION}x -path "$DESTINATION_DIR" ./*.$EXTENSION
FILES_PROCESSED_IN_CURRENT_DIR=`find $DESTINATION_PIC_DIR/$directory -type f -name "*.$extension" -exec printf x \; | wc -c`
TOTAL_FILES_PROCESSED=$((TOTAL_FILES_PROCESSED+FILES_PROCESSED_IN_CURRENT_DIR))
}
finish() {
TOTAL_FILES_IN_SOURCE=`find . -type f -regex ".*\.\($ELIGIBLE_PIC_EXTENSIONS_REGEX\)" -exec printf x \; | wc -c`
UNHANDLED_FILES_COUNT=`find . -type f -not -regex ".*\.\($ELIGIBLE_PIC_EXTENSIONS_REGEX\)" | wc -l`
echo
echo "========================"
echo "Finished processing."
echo "Total images in source: $TOTAL_FILES_IN_SOURCE"
echo "Total images skipped: $SKIPPED_FILES_COUNT"
echo "Total unhandled files: $UNHANDLED_FILES_COUNT"
echo "Total images processed: $TOTAL_FILES_PROCESSED"
}
# MAIN RUN
welcome_prompt
main
finish