bertieb

Images from image: http://superuser.com/questions/939910/

Jul 13th, 2015
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.16 KB | None | 0 0
  1. #!/bin/bash
  2. # resizer.sh - resize target image between two resolutions
  3. # accepts file as either first argument or by setting FILEPATH variable
  4.  
  5. # SETTINGS
  6.  
  7. SMALLEST_WIDTH=73       # px
  8. LARGEST_WIDTH=1000      # px
  9. FILEPATH=               # set if you don't want to pass in image as argument
  10. NUM_OF_RESOLUTIONS=100  # number of images generated; will generate between
  11.                         # $SMALLEST_WIDTH and $LARGEST_WIDTH
  12. RATIO=                  # set if you want to specify width/height
  13.                         # (eg 1/1, 4/3, 16/9), blank is preserve current ratio
  14.  
  15.  
  16. # NOTE: resizing to other aspect ratios may be slow/distorty:
  17. # as per http://www.imagemagick.org/Usage/resize/#noaspect
  18. # Seamless resizing (default) may be preferred, see:
  19. # http://www.imagemagick.org/Usage/resize/#liquid-rescale
  20. # but note it is slower, particularly as images get larger
  21.  
  22. LIQUID=0
  23.  
  24. # SCRIPT BELOW
  25.  
  26. die() { printf "$@\n" 1>&2 ; exit 1; }
  27.  
  28. if [ -z "$FILEPATH" ]; then
  29.         if [ -z "$1" ]; then die "Need to supply file to work on either as argument or by setting FILEPATH!";
  30.         else FILE="$1";
  31.         fi
  32. else
  33.         FILE="$FILEPATH"
  34. fi
  35.  
  36. # check file exists and is regular file
  37.  
  38. if [ ! -e "$FILE" ]; then die "$FILE does not exist!"; fi
  39. if [ ! -f "$FILE" ]; then die "$FILE is not a regular file!"; fi
  40.  
  41. i=0
  42. step=$(echo "($LARGEST_WIDTH - $SMALLEST_WIDTH) / ($NUM_OF_RESOLUTIONS - 1)" | bc -l)
  43. #printf "Resolution step is: %s\n-------------" "$step"
  44. while [ $i -lt $NUM_OF_RESOLUTIONS ]; do
  45.         # handle ratio
  46.         WIDTH=$(echo "$SMALLEST_WIDTH+($step*$i)" | bc -l)
  47.         if [ -z "$RATIO" ]; then
  48.                 #printf "convert %s -resize %s %s\n" "$FILE" "$WIDTH" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
  49.                 convert "$FILE" -resize "$WIDTH" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
  50.         else
  51.                 HEIGHT=$(echo "$WIDTH * $RATIO" | bc -l)
  52.                 if [ "$LIQUID" -eq 0 ]; then
  53.                         # Uncomment convert line for distorted ("squashed") resizing
  54.                         #printf "convert %s -resize %sx%s\! %s\n" "$FILE" "$WIDTH" "$HEIGHT" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
  55.                         convert "$FILE" -resize "$WIDTH"x"$HEIGHT"\! "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
  56.                 else
  57.                         # Liquid resizing: http://www.imagemagick.org/Usage/resize/#liquid-rescale
  58.                         # fast aspect ration resize first, then liquid
  59.                         #printf "convert %s -resize %s %s\n" "$FILE" "$WIDTH" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
  60.                         convert "$FILE" -resize "$WIDTH" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
  61.                         #printf "%s details are now:\n %s\n" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}" "$(identify "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}")"
  62.                         #printf "convert %s -liquid-rescale %sx%s\! %s\n" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}" "$WIDTH" "$HEIGHT" "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
  63.                         convert "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}" -liquid-rescale "$WIDTH"x"$HEIGHT"\! "${FILE%.*}-${WIDTH%.*}px.${FILE##*.}"
  64.                 fi
  65.         fi
  66.         (( i++ ))
  67. done
Add Comment
Please, Sign In to add comment