Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- if [ -z "$1" ] || [ -z "$2" ]
- then echo "
- Usage: script [dir name] [quality] <options>
- Arguments:
- directory name - script take all jpg files from dir
- quality threshold - jpg quality value (0-100); if file has higher quality will be processed lossy, otherwise lossless
- additional options (optional)
- Default options are: p,s
- p - preserve file timestamp
- s - strip all markers (xmp, iptc etc.)
- Example:
- ./compress-jpg.sh img-test/ 93 -n
- Due to bash oddities, better have dirs and files without white spaces or parentheses. I tried to handle this but... this is
- bash.
- "
- else
- # Progressive option is inefficient for small images; that is "normal" will make smaller files
- PROGRESSIVE_THRESHOLD=10240
- dirName=$1
- qualityThreshold=$2
- additionalOptions=$3
- startDirSize=$(du -s $dirName | cut -f 1)
- touch error.log
- rm error.log
- find $dirName -type f -print0 | while read -d $'\0' file; do
- # Escaping white spaces
- file=$(echo $file | sed 's/ /\\ /g')
- # Escaping parentheses
- file=$(echo $file | sed 's/[(]/\\(/g;s/[)]/\\)/g')
- qualityValue=$(($(identify -verbose "$file" | grep 'Quality:' | sed 's/[^0-9]//g')))
- fileSize=$(stat --printf="%s" "$file")
- optionProgressive=" "
- if (($fileSize > $PROGRESSIVE_THRESHOLD)); then
- optionProgressive="--all-progressive"
- else
- optionProgressive="--all-normal"
- fi
- optionQuality=" "
- if (($qualityValue > $qualityThreshold)); then
- optionQuality="--max=$qualityThreshold"
- fi
- command="jpegoptim -p -s $optionQuality $optionProgressive $additionalOptions $file"
- echo $command
- eval $command 2>> error.log
- echo "----------"
- done;
- echo "=========="
- endSize=$(du -s $dirName | cut -f 1)
- diffSize=$(($startDirSize - $endSize))
- diffPercent=$(($(($diffSize*100))/$startDirSize));
- echo "Saved $diffSize KB ($diffPercent%)"
- touch error.log
- printf "Errors:\n" && cat error.log
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement