Resize all JPEG files in current directory, to size (Needs ImageMagick installed, for ‘convert’)


#!/bin/bash
 function shw_grey { echo -e '33[1;30m'"$1"'33[0m'; }
 function shw_norm { echo "$1"; }
 function shw_info { echo -e '33[1;34m'"$1"'33[0m'; }
 function shw_warn { echo -e '33[1;33m'"$1"'33[0m'; }
 function shw_err { echo -e '33[1;31mERROR: '"$1"'33[0m'; }
function shw_usage {
 cat <<EOT
 Resize all JPEG files in current directory, to  size
 (Needs ImageMagick installed, for 'convert')
Usage: $0 []
 where  is described in http://www.imagemagick.org/script/command-line-processing.php#geometry and can be one of:
 scale% Height and width both scaled by specified percentage.
 scale-x%xscale-y% Height and width individually scaled by specified percentages. (Only one % symbol needed.)
 width Width given, height automagically selected to preserve aspect ratio.
 xheight Height given, width automagically selected to preserve aspect ratio.
 widthxheight Maximum values of height and width given, aspect ratio preserved.
 widthxheight^ Minimum values of width and height given, aspect ratio preserved.
 widthxheight! Width and height emphatically given, original aspect ratio ignored.
 widthxheight> Shrinks an image with dimension(s) larger than the corresponding width and/or height argument(s).
 widthxheight< Enlarges an image with dimension(s) smaller than the corresponding width and/or height argument(s).
 area@ Resize image to have specified area in pixels. Aspect ratio is preserved.
 defaults to "2000x2000" (max width 2000 or max height 2000, aspect ratio preserved)
Examples:
 $0 50%
 $0 2000x2000
EOT
 }
shw_usage
# $0 -h|--help
 [[ "$1" =~ -h ]] && exit 255
#set GEOMETRY
 if [[ $1 ]]; then
 GEOMETRY=$1
 else
 GEOMETRY="2000x2000"
 fi
#Are you sure
 shw_warn "This will resize all fotos in current dir to '$GEOMETRY'"
 shw_warn "Are you sure ? yes/[no]"
 read ANSWER
 [[ "$ANSWER" != "yes" ]] && exit 254
TMP_DIR=$(mktemp -d)
 for FILE in $(ls -1 . | egrep -i '\.(jpg|jpeg)$'); do
 TMP_FILE=$TMP_DIR/$(basename $FILE)
 shw_info ".....resizing $FILE"
 convert "$FILE" -resize "$GEOMETRY" "$TMP_FILE" && mv -f "$TMP_FILE" "$FILE"
 done
 shw_info "Finished"
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: