-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdlogo.sh
111 lines (89 loc) · 3.89 KB
/
stdlogo.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
#!/bin/bash
stdlogo() {
local input=""
local output=""
local batch_mode=false
local output_width=400
local output_height=230
local logo_width=230
local logo_height=85
local height_tolerance=0
local fuzz=0
local background="white"
local workdir=$(mktemp -d -t stdlogo-XXXX)
local quiet=false
calc() { awk "BEGIN{print int($*)}"; }
log() {
if [ "$quiet" = false ]; then
echo $1
fi
}
# Parse command-line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
-i|--input) input="$2"; shift ;;
-o|--output) output="$2"; shift ;;
--fuzz) fuzz="$2"; shift ;;
--width) output_width="$2"; shift ;;
--height) output_height="$2"; shift ;;
--tolerance) height_tolerance="$2"; shift ;;
--logo-width) logo_width="$2"; shift ;;
--logo-height) logo_height="$2"; shift ;;
--background) desired_height="$2"; shift ;;
--workdir) workdir="$2"; shift ;;
--batch) batch_mode=true ;;
q|--quiet) quiet=true ;;
*) echo "Unknown parameter passed: $1"; return 1 ;;
esac
shift
done
if [ -z "$input" ]; then
echo "Error: No input file or directory provided."
return 1
fi
if [ -z "$output" ]; then
echo "Error: No output directory provided."
return 1
fi
if [ "$batch_mode" = true ]; then
log "Using batch mode"
for f in $input/*.{jpg,jpeg,JPG,JPEG,png,PNG,svg,SVG,webp,WEBP}; do
if [ -e "$f" ]; then
log "Proccessing $f"
stdlogo -i $f --fuzz $fuzz --width $output_width --height $output_height --tolerance $height_tolerance --logo-width $logo_width --logo-height $logo_height --background $background --workdir $workdir -o $output
fi
done
else
# Copy the source file to workdir
cp $input $workdir/
# Get filename from path
input=$(basename ${input})
# Flatten and convert the image to JPEG format with given background
if [[ ! ${input##*.} =~ ^(jpg|jpeg|JPG|JPEG)$ ]]; then
log "${input##*.} file detected. Flattening and converting ${input} to jpeg"
convert $workdir/$input -background $background -flatten -alpha off "$workdir/${input%.${input##*.}}.jpg"
rm $workdir/$input
fi
# Trim the image based on fuzz percentage
log "Trimming logo..."
mogrify "$workdir/${input%.${input##*.}}.jpg" -fuzz "${fuzz}%" -trim +repage "$workdir/${input%.${input##*.}}.jpg"
# Get the width and height of the image
local width=$(identify -format '%w' "$workdir/${input%.${input##*.}}.jpg")
local height=$(identify -format '%h' "$workdir/${input%.${input##*.}}.jpg")
# Calculate the scaled height
local scaled_height=$(calc "($logo_width/$width)*$height")
# Compare the scaled height with the desired logo height and tolerance
log "Scaling logo..."
if [ "$scaled_height" -lt "$(calc $logo_height + $height_tolerance)" ]; then
# Resize and extend the image with logo width as the primary dimension
mogrify "$workdir/${input%.${input##*.}}.jpg" -geometry "${logo_width}x" -gravity center -background "$background" -extent "${output_width}x${output_height}" "$workdir/${input%.${input##*.}}.jpg"
else
# Resize and extend the image with logo height as the primary dimension
mogrify "$workdir/${input%.${input##*.}}.jpg" -geometry "x${logo_height}" -gravity center -background "$background" -extent "${output_width}x${output_height}" "$workdir/${input%.${input##*.}}.jpg"
fi
cp "$workdir/${input%.${input##*.}}.jpg" "$output/"
fi
unset -f calc
}
stdlogo "$@"
rm -rf /tmp/stdlogo-*