Created
December 14, 2013 19:57
-
-
Save effrenus/7964109 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
TMPDIR="/tmp/psnr_tmp" | |
PSNR_LEVEL=40 | |
mkdir $TMPDIR | |
ls *.jpg | while read file; do | |
echo $file | |
echo "-----------------" | |
name=${file%%.*} | |
actualsize=$(du -b "$file" | cut -f 1) | |
djpeg -pnm $file >$TMPDIR/$name.pnm | |
for q in {60..90..1} ; do | |
echo -ne "Process quality: $q"\\r | |
cjpeg -quality $q $TMPDIR/$name.pnm >$TMPDIR/$file | |
djpeg -pnm $TMPDIR/$file >$TMPDIR/$name_$q.pnm | |
psnr=$(pnmpsnr $TMPDIR/$name.pnm $TMPDIR/$name_$q.pnm 2>&1 >/dev/null | awk '/Y color/ {print $5}') | |
if [ $(echo "$psnr>=$PSNR_LEVEL" | bc) -eq 1 ]; then | |
jpegtran -copy none -optimize -outfile $TMPDIR/$file $TMPDIR/$file | |
compressedsize=$(du -b "$TMPDIR/$file" | cut -f 1) | |
if [ $compressedsize -lt $actualsize ]; then | |
mv $TMPDIR/$file ./compressed/ | |
echo "Quality: $q Result: filesize $actualsize => $compressedsize" | |
else | |
echo "Can't be more lossy comressed with current PSNR level: $PSNR_LEVEL" | |
fi | |
break | |
fi | |
done | |
done | |
rm -r $TMPDIR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment