-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvydGenerateImage
More file actions
executable file
·160 lines (145 loc) · 4.7 KB
/
vydGenerateImage
File metadata and controls
executable file
·160 lines (145 loc) · 4.7 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
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
#!/bin/bash
# Define some colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Initialize total elapsed time
TOTAL_START_TIME=$(date +%s)
# Check if ImageMagick is installed
if ! command -v convert &> /dev/null; then
echo -e "${RED}ImageMagick could not be found, please install it.${NC}"
exit 1
fi
# Default settings
width=""
height=""
object_fit="fill" # Corresponds to CSS 'object-fit'
format=""
overwrite=false
backup_dir=""
declare -a qualities # Array to store multiple quality values
declare -a input_images # Array to store multiple input images
# Usage message
function usage {
echo -e "${YELLOW}Usage: $0 [-i <input_image>]... [-w <width>] [-h <height>] [-of <object_fit>] [-f <format>] [-q <quality>]... [-o [backup_dir]]${NC}"
echo ""
echo "Options:"
echo -e " ${GREEN}-i${NC} Input image file or directory (can be specified multiple times)"
echo -e " ${GREEN}-w${NC} Width (optional)"
echo -e " ${GREEN}-h${NC} Height (optional)"
echo -e " ${GREEN}-of${NC} Object fit option (fill, contain, cover, scale-down, none) (optional)"
echo -e " ${GREEN}-f${NC} Format (jpg, png, etc.) (optional)"
echo -e " ${GREEN}-q${NC} Quality (1-100) (can be specified multiple times, optional)"
echo -e " ${GREEN}-o${NC} Overwrite original files (destructive). If a directory name is provided, backs up originals first."
echo ""
exit 1
}
# Parse command-line options
while getopts ':i:w:h:of:f:q:o::' flag; do
case "${flag}" in
i) input_images+=("${OPTARG}") ;;
w) width="${OPTARG}" ;;
h) height="${OPTARG}" ;;
of) object_fit="${OPTARG}" ;;
f) format="${OPTARG}" ;;
q) qualities+=("${OPTARG}") ;;
o) overwrite=true; backup_dir="${OPTARG}" ;;
:) echo -e "${RED}Option -$OPTARG requires an argument.${NC}" >&2; exit 1 ;;
\?) usage ;;
esac
done
# Check mandatory input
if [ ${#input_images[@]} -eq 0 ]; then
echo -e "${RED}At least one input image is required${NC}"
usage
fi
function process_image {
input_image="$1"
base_filename="${input_image%.*}"
extension="${input_image##*.}"
if [ -n "$format" ]; then
extension="$format"
fi
declare -a convert_cmd=("convert" "$input_image")
# Add resizing options based on object-fit equivalent
if [ -n "$width" ] && [ -n "$height" ]; then
case "$object_fit" in
fill)
convert_cmd+=("-resize" "${width}x${height}^")
convert_cmd+=("-gravity" "center" "-extent" "${width}x${height}")
;;
contain)
convert_cmd+=("-resize" "${width}x${height}")
;;
cover)
convert_cmd+=("-resize" "${width}x${height}^")
convert_cmd+=("-gravity" "center" "-crop" "${width}x${height}+0+0" "+repage")
;;
scale-down)
convert_cmd+=("-resize" "${width}x${height}>")
;;
none)
;; # Do nothing
*)
echo -e "${RED}Invalid object-fit option: $object_fit${NC}"
exit 1
;;
esac
elif [ -n "$width" ]; then
convert_cmd+=("-resize" "${width}")
elif [ -n "$height" ]; then
convert_cmd+=("-resize" "x${height}")
fi
# Process each specified quality
if [ ${#qualities[@]} -gt 0 ]; then
for quality in "${qualities[@]}"; do
if [ "$overwrite" = true ]; then
if [ -n "$backup_dir" ]; then
mkdir -p "$backup_dir"
cp "$input_image" "$backup_dir/"
fi
output_filename="$input_image"
else
output_filename="${base_filename}_${width}w-${height}h-of_${object_fit}_q-${quality}.${extension}"
fi
current_cmd=("${convert_cmd[@]}")
current_cmd+=("-quality" "$quality")
"${current_cmd[@]}" "$output_filename"
done
else
if [ "$overwrite" = true ]; then
if [ -n "$backup_dir" ]; then
mkdir -p "$backup_dir"
cp "$input_image" "$backup_dir/"
fi
output_filename="$input_image"
else
output_filename="${base_filename}_${width}w-${height}h-of_${object_fit}.${extension}"
fi
"${convert_cmd[@]}" "$output_filename"
fi
}
# New section to prepare files for processing
declare -a all_files
for input_item in "${input_images[@]}"; do
if [ -d "$input_item" ]; then
for file in "$input_item"/*; do
if [[ $file =~ \.(jpg|jpeg|png|gif)$ ]]; then # Add more extensions if needed
all_files+=("$file")
fi
done
elif [ -f "$input_item" ]; then
all_files+=("$input_item")
else
echo -e "${RED}Input '$input_item' is not a valid file or directory.${NC}"
fi
done
# Process all collected files
for file in "${all_files[@]}"; do
process_image "$file"
done
# Calculate elapsed time for this job
JOB_END_TIME=$(date +%s)
JOB_ELAPSED_TIME=$((JOB_END_TIME - TOTAL_START_TIME))
echo -e "${YELLOW}Total elapsed time: $JOB_ELAPSED_TIME seconds${NC}"