add option for jpeg quality
[anondcim.git] / anondcim
index 5684bf1f3a9308e1c1bcfe94ec640bab014d3f1c..3396810999a59e6fd921d3f0c00d2b5948106512 100755 (executable)
--- a/anondcim
+++ b/anondcim
-#!/bin/sh -e
+#!/bin/dash
 
-function die() { 
-    echo -e "$*" >&2
-    exit 1
+# anondcim
+# forked from http://code.sotun.de/git/anondcim/
+#
+# copyright 2012-2014 Jan Huwald, Felix Kästner (fpunktk)
+#
+# 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 3 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, see <http://www.gnu.org/licenses/>.
+
+err() {
+    echo "$*" >&2
 }
 
-function r() {
-    echo $(($RANDOM % $1))
+die() {
+    err "$*"
+    exit 1
 }
 
-max=$#
-cur=1
+rand() {
+    # random number between 0 and $1 - 1
+    # read from urandom, sh interpretes numbers with leading 0 wrong, so prepend $1 which doesn't change the result
+    echo $(( $1$(tr -dc "0-9" < /dev/urandom | head -c $((${#1} + 2))) % $1 ))
+}
 
-[ $max -gt 0 ] || die "Usage:\n[IMG_PREFIX=your_prefix] $0 img1.jpg img2.jpg ..."
-[ -x "$(which convert)" ] || die "ImageMagick is not installed"
-[ -x "$(which jhead)" ] || die "jhead is not installed"
+type convert >/dev/null 2>&1 || die "ImageMagick (convert, identify) is not installed"
+type jhead 1>/dev/null 2>&1 || type exiftool 1>/dev/null 2>&1 || die "jhead or exiftool has to be installed"
 
-while [ $# -gt 0 ]; do
-    dst="$IMG_PREFIX$(seq -w $cur $max|head -n1).jpg"
-    echo -e "[$((100*cur/max))%]\t$1\t-> $dst"
+# set default values
+file_prefix=""
+dst_size="1920"
+dst_dir="--same--"
+jpg_quality="75"
 
-    [ -f "$1" ] || die "$1 does not exist"
-    (! [ -e "$dst" ]) || die "$dst already exists"
+[ $# -gt 0 ] || die "Usage: $0 [-p image_file_prefix] [-s destination_size (absolute or percentage, default: $dst_size)] [-d destination_directory] [-q destination_jpg_quality (default: $jpg_quality)] imagefile(s)"
 
-    read W H <<EOF
-        $(identify $1 |cut -f3 -d\ |tr x \ )
-EOF
+while getopts ':p:s:d:q:' OPTION
+do
+    case $OPTION in
+        "p")
+            file_prefix="${OPTARG}_"
+        ;;
+        "s")
+            dst_size="$OPTARG"
+        ;;
+        "d")
+            dst_dir="${OPTARG%/}/" # enforce a trailing "/"
+            [ -d "$dst_dir" ] || die "destination directory \"$dst_dir\" does not exist"
+        ;;
+        "q")
+            jpg_quality="$OPTARG"
+        ;;
+        *)
+            err "not recognised: OPTION=$OPTION, OPTARG=$OPTARG"
+        ;;
+    esac
+done
+shift $(($OPTIND - 1))
 
-    [ $W -ge 100 ] && [ $H -ge 100 ] || die "image is too small"
-    
-    if [ $W -ge 1000 ]; then DW=$((W / 100)); else DW=10; fi
-    if [ $H -ge 1000 ]; then DH=$((H / 100)); else DH=10; fi
-    
-    W=$(($W-1))
-    H=$(($H-1))
-    
-    convert $1 \
-       -colorspace RGB \
-       -distort Perspective "$(
-        (   echo $(r $DW)           $(r $DH)           0  0
-           echo $(($W - $(r $DW))) $(r $DH)           $W 0
-           echo $(r $DW)           $(($H - $(r $DH))) 0  $H
-           echo $(($W - $(r $DW))) $(($H - $(r $DH))) $W $H
-        ) | tr " \n" ", ")" \
-       -filter gaussian -define filter:support=5 -define filter:sigma=0.5 \
-       -attenuate 2 +noise Uniform \
-       -resize 50% \
-       -colorspace sRGB \
-       "$dst"
-
-    jhead -purejpg -q "$dst" || die "removing meta-data failed"
+cur=1
+total=$#
+addend=1
 
+for fn in "$@"
+do
+    [ -f "$fn" ] || { err "source \"$fn\" does not exist and is skipped"; continue; }
+    #
+    if [ "$dst_dir" = "--same--" ]
+    then
+        # use the directory of the source file
+        dst="$(dirname "$fn")/"
+    else
+        dst="$dst_dir"
+    fi
+    #
+    # always use a padded number as destination filename suffix
+    dst_jpg="$dst$file_prefix$(echo -n "0000000000$cur" | tail -c ${#total}).jpg"
+    while [ -e "$dst_jpg" ] && [ $addend -lt 1000 ]
+    do
+        dst_jpg="$dst$file_prefix$(echo -n "0000000000$(($cur + $addend))" | tail -c ${#total}).jpg"
+        addend=$((addend + 1))
+    done
+    [ -e "$dst_jpg" ] && { err "destination \"$dst_jpg\" for source \"$fn\" already exists, anonymization is skipped"; continue; }
+    #
+    echo "anonymizing \"$fn\" to \"$dst_jpg\" ($cur/$total)"
+    #
+    read w h << EOF
+$(identify -format '%w %h' "$fn")
+EOF
+    #
+    # resize and distort
+    if [ $w -ge 100 ] && [ $h -ge 100 ]
+    then
+        if [ $w -ge 1000 ]; then dw=$(($w / 100)); else dw=10; fi
+        if [ $h -ge 1000 ]; then dh=$(($h / 100)); else dh=10; fi
+        #
+        w=$(($w - 1))
+        h=$(($h - 1))
+        #
+        convert "$fn" \
+            -colorspace RGB \
+            -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" \
+            -filter gaussian -define filter:support=5 -define filter:sigma=0.5 \
+            -attenuate 2 +noise Uniform \
+            -resize "$dst_size" \
+            -colorspace sRGB \
+            -quality "$jpg_quality" \
+            "$dst_jpg"
+    else
+        err "image is too small to be distorted and will just be filtered and resized"
+        convert "$fn" \
+            -colorspace RGB \
+            -filter gaussian -define filter:support=5 -define filter:sigma=0.5 \
+            -attenuate 2 +noise Uniform \
+            -resize "$dst_size" \
+            -colorspace sRGB \
+            -quality "$jpg_quality" \
+            "$dst_jpg"
+    fi
+    #
+    # remove metadata
+    if [ -x "$(which jhead)" ]
+    then
+        jhead -purejpg -q "$dst_jpg" || err "removing meta-data with jhead failed"
+    fi
+    if [ -x "$(which exiftool)" ]
+    then
+        exiftool -quiet -overwrite_original -all= "$dst_jpg" || err "removing meta-data with exiftool failed"
+    fi
+    #
     cur=$(($cur + 1))
-    shift
 done
+