-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexifrotate
executable file
·97 lines (85 loc) · 2.55 KB
/
exifrotate
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
#!/bin/sh
#
# exifrotate - a shell script to rotate pictues (and their exif thumbs)
#
# Copyright (C) 2013 Simon Walz, http://sw.nerdbox.de/
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
#
#
# Based on ideas of the exifautotran script,
# see: http://sylvana.net/jpegcrop/exif_orientation.html
#
trap "rm -f \".tempfile.jpg\" \".tempthumb.jpg\" \".tempthumb_rot.jpg\"" INT QUIT TERM
for i
do
case $i in
-h|--help)
echo "exifrotate [files.jpg]"
echo "a shell script to rotate pictures using their exif orientation flag.\n"
echo ""
echo "Example: exifrotate *.jpg"
exit 0;;
esac
echo $i
EXIFORIENT=`exiftool -Orientation -S -n "${i}" | grep -oE "[0-9]+"`
case "$EXIFORIENT" in
1) transform="";;
2) transform="-flip horizontal";;
3) transform="-rotate 180";;
4) transform="-flip vertical";;
5) transform="-transpose";;
6) transform="-rotate 90";;
7) transform="-transverse";;
8) transform="-rotate 270";;
*) transform="";;
esac
if test -n "$transform"; then
echo Rotate image with $transform >&2
jpegtran -copy all $transform "$i" >.tempfile.jpg
if test $? -ne 0; then
echo Error while transforming -- skipped. >&2
else
# delete the orientation flag:
exiftool -n -overwrite_original -Orientation=1 .tempfile.jpg
# extracting the thumbnail image:
exiftool -b -ThumbnailImage "$i" >.tempthumb.jpg
if [ -s ".tempthumb.jpg" ]; then
# rotate the thumbnail image:
jpegtran -copy all $transform \
".tempthumb.jpg" >.tempthumb_rot.jpg
if test $? -ne 0; then
echo Error while transforming thumb.>&2
else
# implant the thumbnail image:
exiftool -overwrite_original \
'-ThumbnailImage<=.tempthumb_rot.jpg' \
.tempfile.jpg
fi
else
echo Error extracting the thumbnail image. >&2
fi
# delete thumbs:
rm .tempthumb.jpg .tempthumb_rot.jpg
# do a backup
#mkdir -p _backup
#mv "$i" _backup/
# delete the orginal file
rm "$i"
# replace origingal file
mv .tempfile.jpg "$i"
fi
fi
done