Skip to content

Instantly share code, notes, and snippets.

@othree
Last active February 1, 2016 11:09
Show Gist options
  • Save othree/b9254f0ebf7d9fef0e23 to your computer and use it in GitHub Desktop.
Save othree/b9254f0ebf7d9fef0e23 to your computer and use it in GitHub Desktop.
#!/bin/bash
Q=85
while [[ $# > 1 ]]
do
key="$1"
if [ $2 ]
then
case $key in
-q|--quality)
Q="$2"
shift # past argument
;;
-s|--sample)
SAMPLE="$2"
shift # past argument
;;
-t|--quant-table)
QUANT_TABLE="$2"
shift # past argument
;;
*)
# unknown option
;;
esac
fi
shift # past argument or value
done
INPUT="$1"
if [ ! $INPUT ]
then
echo "Usage: png2mozj [options] <png-image>"
echo "Options:"
echo " -q, --quality e.g. 95"
echo " -s, --sample e.g. 1x1"
echo " -t, --quant-table e.g. 2"
echo ""
exit 0
fi
OUTPUT=`echo $INPUT | sed 's/png$/jpg/'`
if [[ -x `which mozcjpeg` ]]
then
EXE="mozcjpeg"
elif [[ -x `which mozjpeg` ]]
then
EXE="mozjpeg"
elif [[ -x `which cjpeg` ]]
then
EXE="cjpeg"
else
echo "Executable not found"
exit 1
fi
args=()
if [ $Q ]
then
args+=("-quality")
args+=($Q)
fi
if [ $SAMPLE ]
then
args+=("-sample")
args+=($SAMPLE)
fi
if [ $QUANT_TABLE ]
then
args+=("-quant-table")
args+=($QUANT_TABLE)
fi
AA=$(IFS=" " ; echo "${args[*]}")
echo "convert $INPUT pnm:- | $EXE $AA > $OUTPUT"
convert $INPUT pnm:- | $EXE $AA > $OUTPUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment