Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # resizer.sh - resize target image between two resolutions
- # accepts file as either first argument or by setting FILEPATH variable
- # SETTINGS
- SMALLEST_WIDTH=73 # px
- LARGEST_WIDTH=1000 # px
- FILEPATH= # set if you don't want to pass in image as argument
- NUM_OF_RESOLUTIONS=100 # number of images generated; will generate between
- # $SMALLEST_WIDTH and $LARGEST_WIDTH
- RATIO= # set if you want to specify width/height
- # (eg 1/1, 4/3, 16/9), blank is preserve current ratio
- # NOTE: resizing to other aspect ratios may be slow/distorty:
- # as per http://www.imagemagick.org/Usage/resize/#noaspect
- # Seamless resizing (default) may be preferred, see:
- # http://www.imagemagick.org/Usage/resize/#liquid-rescale
- # but note it is slower, particularly as images get larger
- LIQUID=0
- # SCRIPT BELOW
- die() { printf "$@\n" 1>&2 ; exit 1; }
- if [ -z "$FILEPATH" ]; then
- if [ -z "$1" ]; then die "Need to supply file to work on either as argument or by setting FILEPATH!";
- else FILE="$1";
- fi
- else
- FILE="$FILEPATH"
- fi
- # check file exists and is regular file
- if [ ! -e "$FILE" ]; then die "$FILE does not exist!"; fi
- if [ ! -f "$FILE" ]; then die "$FILE is not a regular file!"; fi
- i=0
- step=$(echo "($LARGEST_WIDTH - $SMALLEST_WIDTH) / ($NUM_OF_RESOLUTIONS - 1)" | bc -l)
- #printf "Resolution step is: %s\n-------------" "$step"
- while [ $i -lt $NUM_OF_RESOLUTIONS ]; do
- # handle ratio
- WIDTH=$(echo "$SMALLEST_WIDTH+($step*$i)" | bc -l)
- if [ -z "$RATIO" ]; then
- #printf "convert %s -resize %s %s\n" "$FILE" "$WIDTH" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
- convert "$FILE" -resize "$WIDTH" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
- else
- HEIGHT=$(echo "$WIDTH * $RATIO" | bc -l)
- if [ "$LIQUID" -eq 0 ]; then
- # Uncomment convert line for distorted ("squashed") resizing
- #printf "convert %s -resize %sx%s\! %s\n" "$FILE" "$WIDTH" "$HEIGHT" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
- convert "$FILE" -resize "$WIDTH"x"$HEIGHT"\! "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
- else
- # Liquid resizing: http://www.imagemagick.org/Usage/resize/#liquid-rescale
- # fast aspect ration resize first, then liquid
- #printf "convert %s -resize %s %s\n" "$FILE" "$WIDTH" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
- convert "$FILE" -resize "$WIDTH" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
- #printf "%s details are now:\n %s\n" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}" "$(identify "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}")"
- #printf "convert %s -liquid-rescale %sx%s\! %s\n" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}" "$WIDTH" "$HEIGHT" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
- convert "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}" -liquid-rescale "$WIDTH"x"$HEIGHT"\! "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
- fi
- fi
- (( i++ ))
- done
Add Comment
Please, Sign In to add comment