Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. #!/bin/bash
  2. if [ -z "$1" ] || [ -z "$2" ]
  3. then echo "
  4. Usage: script [dir name] [quality] <options>
  5. Arguments:
  6. directory name - script take all jpg files from dir
  7. quality threshold - jpg quality value (0-100); if file has higher quality will be processed lossy, otherwise lossless
  8. additional options (optional)
  9.  
  10. Default options are: p,s
  11. p - preserve file timestamp
  12. s - strip all markers (xmp, iptc etc.)
  13.  
  14. Example:
  15. ./compress-jpg.sh img-test/ 93 -n
  16.  
  17. Due to bash oddities, better have dirs and files without white spaces or parentheses. I tried to handle this but... this is
  18. bash.
  19. "
  20. else
  21. # Progressive option is inefficient for small images; that is "normal" will make smaller files
  22. PROGRESSIVE_THRESHOLD=10240
  23. dirName=$1
  24. qualityThreshold=$2
  25. additionalOptions=$3
  26. startDirSize=$(du -s $dirName | cut -f 1)
  27. touch error.log
  28. rm error.log
  29.  
  30. find $dirName -type f -print0 | while read -d $'\0' file; do
  31. # Escaping white spaces
  32. file=$(echo $file | sed 's/ /\\ /g')
  33. # Escaping parentheses
  34. file=$(echo $file | sed 's/[(]/\\(/g;s/[)]/\\)/g')
  35.  
  36. qualityValue=$(($(identify -verbose "$file" | grep 'Quality:' | sed 's/[^0-9]//g')))
  37. fileSize=$(stat --printf="%s" "$file")
  38.  
  39. optionProgressive=" "
  40. if (($fileSize > $PROGRESSIVE_THRESHOLD)); then
  41. optionProgressive="--all-progressive"
  42. else
  43. optionProgressive="--all-normal"
  44. fi
  45.  
  46. optionQuality=" "
  47. if (($qualityValue > $qualityThreshold)); then
  48. optionQuality="--max=$qualityThreshold"
  49. fi
  50.  
  51. command="jpegoptim -p -s $optionQuality $optionProgressive $additionalOptions $file"
  52. echo $command
  53. eval $command 2>> error.log
  54. echo "----------"
  55. done;
  56. echo "=========="
  57. endSize=$(du -s $dirName | cut -f 1)
  58. diffSize=$(($startDirSize - $endSize))
  59. diffPercent=$(($(($diffSize*100))/$startDirSize));
  60. echo "Saved $diffSize KB ($diffPercent%)"
  61. touch error.log
  62. printf "Errors:\n" && cat error.log
  63. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement