use err() in die()
[anondcim.git] / anondcim
1 #!/bin/dash
2
3 # anondcim
4 # forked from http://code.sotun.de/git/anondcim/
5 #
6 # copyright 2012-2014 Jan Huwald, Felix Kästner (fpunktk)
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 err() {
22     echo "$*" >&2
23 }
24
25 die() {
26     err "$*"
27     exit 1
28 }
29
30 rand() {
31     # random number between 0 and $1 - 1
32     # read from urandom, sh interpretes numbers with leading 0 wrong, so prepend $1 which doesn't change the result
33     echo $(( $1$(tr -dc "0-9" < /dev/urandom | head -c $((${#1} + 1))) % $1 ))
34 }
35
36 [ $# -gt 0 ] || die "Usage: $0 [-p image_prefix] [-s destination_size (absolute or percentage)] [-d destination_directory] imagefile(s)"
37
38 [ ! -x "$(which convert)" ] && die "ImageMagick (convert, identify) is not installed"
39 [ ! -x "$(which jhead)" ] && [ ! -x "$(which exiftool)" ] && die "jhead or exiftool has to be installed"
40
41 # set default values
42 file_prefix=""
43 dst_size="1280"
44 dst_dir="--same--"
45
46 while getopts ':p:s:d:' OPTION
47 do
48     case $OPTION in
49         "p")
50             file_prefix="${OPTARG}_"
51         ;;
52         "s")
53             dst_size="$OPTARG"
54         ;;
55         "d")
56             # enforce a trailing "/"
57             [ -d "${OPTARG%/}/" ] && dst_dir="${OPTARG%/}/" || die "destination directory \"${OPTARG%/}/\" does not exist"
58         ;;
59         *)
60             err "not recognised: OPTION=$OPTION, OPTARG=$OPTARG"
61         ;;
62     esac
63 done
64 shift $(($OPTIND - 1))
65
66 cur=1
67 total=$#
68
69 for fn in "$@"
70 do
71     if [ "$dst_dir" = "--same--" ]
72     then
73         # if the filename contains a "/" then use this dir
74         [ "$fn" != "${fn%/*}" ] && dst="${fn%/*}/" || dst=""
75     else
76         dst="$dst_dir"
77     fi
78     # always use a padded number as destination filename suffix
79     dst="$dst$file_prefix$(echo -n "0000000000$cur" | tail -c ${#total}).jpg"
80     echo "anonymizing \"$fn\" to \"$dst\" ($cur/$total)"
81     [ -f "$fn" ] || { err "source \"$fn\" does not exist and is skipped"; continue; }
82     [ -e "$dst" ] && { err "destination \"$dst\" for source \"$fn\" already exists, anonymization is skipped"; continue; }
83     
84     read w h << EOF
85 $(identify -format '%w %h' "$fn")
86 EOF
87     
88     # resize and distort
89     if [ $w -ge 100 ] && [ $h -ge 100 ]
90     then
91         if [ $w -ge 1000 ]; then dw=$(($w / 100)); else dw=10; fi
92         if [ $h -ge 1000 ]; then dh=$(($h / 100)); else dh=10; fi
93         
94         w=$(($w - 1))
95         h=$(($h - 1))
96         
97         convert "$fn" \
98             -colorspace RGB \
99             -distort Perspective "$(rand $dw) $(rand $dh) 0 0, $(($w - $(rand $dw))) $(rand $dh) $w 0, $(rand $dw) $(($h - $(rand $dh))) 0 $h, $(($w - $(rand $dw))) $(($h - $(rand $dh))) $w $h" \
100             -filter gaussian -define filter:support=5 -define filter:sigma=0.5 \
101             -attenuate 2 +noise Uniform \
102             -resize "$dst_size" \
103             -colorspace sRGB \
104             "$dst"
105     else
106         err "image is too small to be distorted and will just be filtered and resized"
107         convert "$fn" \
108             -colorspace RGB \
109             -filter gaussian -define filter:support=5 -define filter:sigma=0.5 \
110             -attenuate 2 +noise Uniform \
111             -resize "$dst_size" \
112             -colorspace sRGB \
113             "$dst"
114     fi
115     
116     # remove metadata
117     if [ -x "$(which jhead)" ]
118     then
119         jhead -purejpg -q "$dst" || err "removing meta-data with jhead failed"
120     fi
121     if [ -x "$(which exiftool)" ]
122     then
123         exiftool -overwrite_original -all= "$dst" || err "removing meta-data with exiftool failed"
124     fi
125
126     cur=$(($cur + 1))
127 done
128