try different destinations if already existing
[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} + 2))) % $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 type convert >/dev/null 2>&1 || die "ImageMagick (convert, identify) is not installed"
39 type jhead 1>/dev/null 2>&1 || type exiftool 1>/dev/null 2>&1 || die "jhead or exiftool has to be installed"
40
41 # set default values
42 file_prefix=""
43 dst_size="1920"
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 addend=1
69
70 for fn in "$@"
71 do
72     [ -f "$fn" ] || { err "source \"$fn\" does not exist and is skipped"; continue; }
73     #
74     if [ "$dst_dir" = "--same--" ]
75     then
76         # if the filename contains a "/" then use this dir
77         [ "$fn" != "${fn%/*}" ] && dst="${fn%/*}/" || dst=""
78     else
79         dst="$dst_dir"
80     fi
81     # always use a padded number as destination filename suffix
82     dst_jpg="$dst$file_prefix$(echo -n "0000000000$cur" | tail -c ${#total}).jpg"
83     while [ -e "$dst_jpg" ] && [ $addend -lt 1000 ]
84     do
85         dst_jpg="$dst$file_prefix$(echo -n "0000000000$(($cur + $addend))" | tail -c ${#total}).jpg"
86         addend=$((addend + 1))
87     done
88     [ -e "$dst_jpg" ] && { err "destination \"$dst_jpg\" for source \"$fn\" already exists, anonymization is skipped"; continue; }
89     #
90     echo "anonymizing \"$fn\" to \"$dst_jpg\" ($cur/$total)"
91     #
92     read w h << EOF
93 $(identify -format '%w %h' "$fn")
94 EOF
95     
96     # resize and distort
97     if [ $w -ge 100 ] && [ $h -ge 100 ]
98     then
99         if [ $w -ge 1000 ]; then dw=$(($w / 100)); else dw=10; fi
100         if [ $h -ge 1000 ]; then dh=$(($h / 100)); else dh=10; fi
101         
102         w=$(($w - 1))
103         h=$(($h - 1))
104         
105         convert "$fn" \
106             -colorspace RGB \
107             -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" \
108             -filter gaussian -define filter:support=5 -define filter:sigma=0.5 \
109             -attenuate 2 +noise Uniform \
110             -resize "$dst_size" \
111             -colorspace sRGB \
112             "$dst"
113     else
114         err "image is too small to be distorted and will just be filtered and resized"
115         convert "$fn" \
116             -colorspace RGB \
117             -filter gaussian -define filter:support=5 -define filter:sigma=0.5 \
118             -attenuate 2 +noise Uniform \
119             -resize "$dst_size" \
120             -colorspace sRGB \
121             "$dst"
122     fi
123     
124     # remove metadata
125     if [ -x "$(which jhead)" ]
126     then
127         jhead -purejpg -q "$dst" || err "removing meta-data with jhead failed"
128     fi
129     if [ -x "$(which exiftool)" ]
130     then
131         exiftool -overwrite_original -all= "$dst" || err "removing meta-data with exiftool failed"
132     fi
133
134     cur=$(($cur + 1))
135 done
136