shrink-image.sh 848 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #! /usr/bin/env bash
  2. POSITIONAL_ARGS=()
  3. SIZE="1M"
  4. RESIZE=""
  5. while [[ $# -gt 0 ]]; do
  6. case $1 in
  7. -s|--size)
  8. SIZE="$2"
  9. shift # arg
  10. shift # val
  11. ;;
  12. -r|--resize)
  13. RESIZE="$2"
  14. shift # arg
  15. shift # val
  16. ;;
  17. -*)
  18. echo "Unknown option $1"
  19. exit 1
  20. ;;
  21. *)
  22. POSITIONAL_ARGS+=("$1")
  23. shift
  24. ;;
  25. esac
  26. done
  27. NAME="${POSITIONAL_ARGS[0]}"
  28. if [ "$NAME" == "" ]; then
  29. echo "name empty"
  30. exit 1
  31. fi
  32. if [ ! -x "$NAME" ]; then
  33. echo "does not exist"
  34. exit 1
  35. fi
  36. echo "processing ${NAME@Q} with imagemagick"
  37. JPEG=$(echo "$NAME" | sed 's/ /_/g' | sed 's/[.]tiff$/.jpg/')
  38. DO_RESIZE=""
  39. if [ ! "$RESIZE" == "" ]; then
  40. DO_RESIZE="-resize $RESIZE"
  41. fi
  42. CMD="magick -verbose -define jpeg:extent=$SIZE ${NAME@Q} $DO_RESIZE ${JPEG@Q}"
  43. echo "$CMD"
  44. eval "$CMD"