Skip to content

Instantly share code, notes, and snippets.

@zipizap
Last active December 28, 2015 01:59
Show Gist options
  • Save zipizap/7424940 to your computer and use it in GitHub Desktop.
Save zipizap/7424940 to your computer and use it in GitHub Desktop.
Resize all JPEG files in current directory, to <GEOMETRY> size
#!/bin/bash
function shw_grey { echo -e '\033[1;30m'"$1"'\033[0m'; }
function shw_norm { echo "$1"; }
function shw_info { echo -e '\033[1;34m'"$1"'\033[0m'; }
function shw_warn { echo -e '\033[1;33m'"$1"'\033[0m'; }
function shw_err { echo -e '\033[1;31mERROR: '"$1"'\033[0m'; }
function shw_usage {
cat <<EOT
Resize all JPEG files in current directory, to <GEOMETRY> size
(Needs ImageMagick installed, for 'convert')
Usage: $0 [<GEOMETRY>]
where <GEOMETRY> 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.
<GEOMETRY> 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)
OLD_IFS=$IFS
IFS=$'\n'
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
IFS=$OLD_IFS
rm -rf $TMP_DIR
shw_info "Finished"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment