add option for jpeg quality
[anondcim.git] / anondcim
index aa690a8c7856d503d5cda777b01a5ab11c000225..3396810999a59e6fd921d3f0c00d2b5948106512 100755 (executable)
--- a/anondcim
+++ b/anondcim
@@ -33,8 +33,6 @@ rand() {
     echo $(( $1$(tr -dc "0-9" < /dev/urandom | head -c $((${#1} + 2))) % $1 ))
 }
 
-[ $# -gt 0 ] || die "Usage: $0 [-p image_prefix] [-s destination_size (absolute or percentage)] [-d destination_directory] imagefile(s)"
-
 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"
 
@@ -42,8 +40,11 @@ type jhead 1>/dev/null 2>&1 || type exiftool 1>/dev/null 2>&1 || die "jhead or e
 file_prefix=""
 dst_size="1920"
 dst_dir="--same--"
+jpg_quality="75"
+
+[ $# -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)"
 
-while getopts ':p:s:d:' OPTION
+while getopts ':p:s:d:q:' OPTION
 do
     case $OPTION in
         "p")
@@ -53,8 +54,11 @@ do
             dst_size="$OPTARG"
         ;;
         "d")
-            # enforce a trailing "/"
-            [ -d "${OPTARG%/}/" ] && dst_dir="${OPTARG%/}/" || die "destination directory \"${OPTARG%/}/\" does not exist"
+            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"
@@ -73,11 +77,12 @@ do
     #
     if [ "$dst_dir" = "--same--" ]
     then
-        # if the filename contains a "/" then use this dir
-        [ "$fn" != "${fn%/*}" ] && dst="${fn%/*}/" || dst=""
+        # 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 ]
@@ -92,16 +97,16 @@ do
     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" \
@@ -109,7 +114,8 @@ EOF
             -attenuate 2 +noise Uniform \
             -resize "$dst_size" \
             -colorspace sRGB \
-            "$dst"
+            -quality "$jpg_quality" \
+            "$dst_jpg"
     else
         err "image is too small to be distorted and will just be filtered and resized"
         convert "$fn" \
@@ -118,19 +124,20 @@ EOF
             -attenuate 2 +noise Uniform \
             -resize "$dst_size" \
             -colorspace sRGB \
-            "$dst"
+            -quality "$jpg_quality" \
+            "$dst_jpg"
     fi
-    
+    #
     # remove metadata
     if [ -x "$(which jhead)" ]
     then
-        jhead -purejpg -q "$dst" || err "removing meta-data with jhead failed"
+        jhead -purejpg -q "$dst_jpg" || err "removing meta-data with jhead failed"
     fi
     if [ -x "$(which exiftool)" ]
     then
-        exiftool -overwrite_original -all= "$dst" || err "removing meta-data with exiftool failed"
+        exiftool -quiet -overwrite_original -all= "$dst_jpg" || err "removing meta-data with exiftool failed"
     fi
-
+    #
     cur=$(($cur + 1))
 done