Skip to content

Instantly share code, notes, and snippets.

@rskvazh
Created January 30, 2017 15:30
Show Gist options
  • Save rskvazh/3c83951504d15248c988113960af65a3 to your computer and use it in GitHub Desktop.
Save rskvazh/3c83951504d15248c988113960af65a3 to your computer and use it in GitHub Desktop.
Testing jpeg compressors & parameters with DSSIM
#!/bin/bash
rm -R im_*
mkdir im_orig im_jpg im_jpg_ss im_jpg_ss_prog im_jpg_moz im_jpg_moz2
mkdir im_png
mkdir im_webp
i=0
for file in orig/*.jpg; do
read width height < <(identify -format '%w %h' $file)
if (( $width / $height >= 2 )); then
targetSize="6000x800>"
elif (( $height / $width >= 2 )); then
targetSize="800x6000>"
else
targetSize="800x800>"
fi
fileOut=`basename $file`
convert -resize "$targetSize" -strip $file ./im_png/$fileOut.png &
convert -resize "$targetSize" -strip -quality 100 $file ./im_orig/$fileOut &
convert -resize "$targetSize" -strip -quality 90 $file ./im_jpg/$fileOut &
convert -resize "$targetSize" -strip -quality 90 -sampling-factor "2x2,1x1,1x1" $file ./im_jpg_ss/$fileOut &
convert -resize "$targetSize" -strip -quality 90 -sampling-factor "2x2,1x1,1x1" -interlace Plane $file ./im_jpg_ss_prog/$fileOut &
convert -resize "$targetSize" -strip -quality 90 $file -define webp:method=6 -define webp:pass=6 "./im_webp/$fileOut.webp" &
convert -resize "$targetSize" -strip $file pnm:- | /usr/local/opt/mozjpeg/bin/cjpeg -quality 90 -optimize -progressive > ./im_jpg_moz/$fileOut &
convert -resize "$targetSize" -strip $file pnm:- | /usr/local/opt/mozjpeg/bin/cjpeg -quality 90,75 -optimize -progressive -sample 2x2 > ./im_jpg_moz2/$fileOut &
((i+=1))
if ! ((i % 4)); then
echo -n "."
wait
fi
done
echo ""
wait
du -h
#!/bin/bash
rm -R $1_cropped
mkdir $1_cropped
i=0
for file in $1/*; do
fileOut=`basename $file`
convert -strip -crop +0-20 $file $1_cropped/$fileOut.png &
((i+=1))
if ! ((i % 8)); then
echo -n "."
wait
fi
done
echo ""
wait
du -h
#!/usr/bin/env bash
# Usage: ./dssim-compare im_webp .webp
function calculate_dissimilarity () {
# Convert the original JPEG to PNG for DSSIM comparison
# Also base64 it so we can safely store its result in a variable without needing to write the file to disk
# Run the JPEG compressor, pipe its output to convert, create a PNG from the newly compressed JPEG and hand it to DSSIM for comparison - all without creating a file on disk to increase runtime performance
local __current_dissimilarity=$(dssim $1 <(convert "${2}" png:-) | awk '{print $1}')
echo ${__current_dissimilarity}
}
function is_number () {
# re='^[0-9]+$'
re='^[0-9]+([.][0-9]+)?$'
if [[ $1 =~ $re ]] ; then
echo 1
else
echo 0
fi
}
i=0
sum_dssim=0
log_filename="dssim_`basename $1`.log"
echo -n "" > $log_filename
for file1 in im_png/*; do
file2=$1/`basename -s .png $file1`$2
((i+=1))
if ! ((i % 5)); then
echo -n "."
fi
dssim=`calculate_dissimilarity "$file1" "$file2"`
if [[ `is_number "$dssim"` == 1 ]]; then
echo "`basename $file2`: $dssim" >> $log_filename
sum_dssim=`echo "scale=12; $sum_dssim + $dssim" | bc`
fi
done
wait
echo ""
echo "scale=6; $sum_dssim / $i" | bc
convert -resize 692x820 -strip input1.jpg resized1.png
convert -quality 90 resized1.png resized1_90.jpg
convert -quality 95 resized1.png resized1.webp
convert -resize 692x820 -strip -interlace Plane -quality 90 -sampling-factor "2x2,1x1,1x1" input1.jpg resized1.jpg
identify -format "%f: %Q %b %[jpeg:sampling-factor] %[interlace] %wx%h\n" *
jpegtran -copy none -optimize -progressive resized1_strip_90.jpg -outfile resized1_out.jpg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment