-
Notifications
You must be signed in to change notification settings - Fork 0
/
imgconv.sh
executable file
·173 lines (145 loc) · 2.81 KB
/
imgconv.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
# Project: Image converter
# File: imgconv.sh
# Version: 0.1
# Create by: Rom1 <[email protected]> - CANEL - https://www.canel.ch
# Date: 11/04/2018
# Licence: GNU GENERAL PUBLIC LICENSE v3
# Language: Bash
# Description: Script pour convertir des images
exif_data_artist="Musée Bolo"
exif_data_copyright="CC-BY-SA"
dest="$(pwd)/cpy"
logo="$(pwd)/logo.png"
logo_padding=10
logo_size_x=$(identify -format %w ${logo})
logo_size_y=$(identify -format %h ${logo})
exif_modif_data=0
exif_only=0
extension="jpg"
script_aw=0
usage()
{
echo "Usage: $(basename $0) [-ehjopw] [-x <width>] [-y <height>] <file> [...] | <directory>"
echo -e "\t-e\tModify exif"
echo -e "\t-o\tModify only exif"
echo -e "\t-h\thelp"
echo -e "\t-j\tConvert to JPG"
echo -e "\t-p\tConvert to PNG"
echo -e "\t-w\tUse script Autowhite"
echo -e "\t-x <x>\tResize image width"
echo -e "\t-y <y>\tResize image height"
}
exifModifData()
{
file="$1"
[ -z "$file" ] && return 1
exiftool -q -overwrite_original \
-Artist="${exif_data_artist}" \
-Copyright="${exif_data_copyright}" \
${file}
}
convertion()
{
image="$1"
name=$(basename $image)
[ -z "$extension" ] && extension=${name##*.}
converted=${dest}/${name%.*}.${extension}
convert -resize "$px_x"x"$px_y" \
${image} ${converted}
[ $? -ne 0 ] && return 1
[ $script_aw -eq 1 ] && bash autowhite -m 1 ${converted} ${converted}
composite -compose Over -geometry \
+${logo_padding}+$(expr $(identify -format %H ${converted}) - ${logo_size_y} - ${logo_padding}) ${logo} \
${converted} ${converted}
[ $? -ne 0 ] && return 1
return 0
}
modify()
{
file="$1"
echo -n "${file} "
if [ $exif_modif_data -eq 1 ]
then
exifModifData "$file"
[ $? -ne 0 ] && echo "[KO]" && return 1
fi
if [ -f "$file" -a $exif_only -ne 1 ]
then
convertion "$file"
[ $? -ne 0 ] && echo "[KO]" && return 1
fi
echo "[OK]"
}
trap 'echo "CANCEL" ; exit 1' INT
while getopts "ehjopwx:y:" sel
do
case $sel in
e)
exif_modif_data=1
;;
h)
usage
exit 0
;;
j)
extension="jpg"
;;
o)
exif_only=1
;;
p)
extension="png"
;;
w)
script_aw=1
;;
x)
if [[ "$OPTARG" =~ ^[0-9]+$ ]]
then
px_x="$OPTARG"
else
echo "Error: Bad width value"
usage
exit 1
fi
;;
y)
if [[ "$OPTARG" =~ ^[0-9]+$ ]]
then
px_y="$OPTARG"
else
echo "Error: Bad height value"
usage
exit 1
fi
;;
*)
echo "Error: Bad argument"
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
[ -z "$px_x" -a -z "$px_y" ] && px_x=1000
[ ! -d "$dest" -a $exif_only -ne 1 ] && mkdir -p $dest
if [ -d "$1" -a -z "$2" ]
then
for file in $(find ${1} -maxdepth 1 -type f -print)
do
modify "$file"
done
elif [ -f "$1" ]
then
for file
do
modify "$file"
shift
done
else
echo "Error: Choose a directory or file(s)"
usage
exit 1
fi
exit 0