sss: save some space, picture compressor pdfdiff : perform a visual diff on simila pdfs to highlight
150 lines
4.7 KiB
Bash
150 lines
4.7 KiB
Bash
#!/bin/bash
|
|
|
|
# TODO: Read the doc about optipng for further compression ?
|
|
|
|
usage="\
|
|
Usage: $0 [-p PREFIX] [-s SUFFIX] [-e EXTENSION] [-g \"GEOMETRY\"] [-q QUALITY] [-vntP] image1 image2 image3 ...\n
|
|
Use ImageMagick's convert tool to reduce the size of a selection of image files.\n
|
|
\n
|
|
Options:\n
|
|
-p PREFIX\tAdd a prefix to output file name. Can be used to provide a \n
|
|
\t\tdifferent output folder.\n
|
|
-s SUFFIX\tAppend a suffix to the file name, before the extension.\n
|
|
-t\t\tReplace the filename with creation time from exif\n
|
|
-g \"GEOMETRY\"\tSpecify a desired geometry. Default : \"1920x1080>\" \n
|
|
\t\t(see https://www.imagemagick.org/Magick++/Geometry.html\n
|
|
\t\tfor detailled doc)\n
|
|
-e EXTENSION\tSpecify a file extension (recognized by Magick).\n
|
|
\t\tIf not specified, reuse orinal file's extension. \n
|
|
-q QUALITY\tSet the jpeg compression rate in %. Default : 85\n
|
|
-x\t\tStrip exif metadata. Default off\n
|
|
-t\t\tUse exif creation time instead of original filename when renaming.\n
|
|
\t\tCan be used with -p and -s\n
|
|
-S\t\tDeactivate JPEG sampling factor. First level of compression, very little \n
|
|
\t\tloss in quality. Default on (4:2:0)\n
|
|
-m\t\tDeactivate median blurring of the image. This filter can smooth some of\n
|
|
\t\tthe noise with minimal loss in details. \n
|
|
\t\tBetter compression, with some loss of quality. Default on.\n
|
|
-M SIZE\tSpecify the size of the median blurr (useful for fine-tuning text scans).\n
|
|
\t\tDefault: 2\n
|
|
-G\t\tApply gaussian blurr to the image. Smooths noise in background or low-light\n
|
|
\t\timages, but creates a loss of detail. \n
|
|
\t\tBest compression, most loss of quality. Default off\n
|
|
-v\t\tMake verbose\n
|
|
-P\t\tPrint progress (for use with zenity --progress)\n
|
|
-n\t\tNo changes. Simulate only, print the command lines but do not run them.
|
|
-h\t\tPrint this help and exit.\n"
|
|
|
|
if [[ $# == 0 ]] ; then echo -e $usage ; exit 0 ; fi
|
|
|
|
#Preset defaults
|
|
nopt=1
|
|
geom="1920x1080>"
|
|
qual="85"
|
|
ext="jpg"
|
|
verbose=false
|
|
nochange=""
|
|
sampling="-sampling-factor 4:2:0"
|
|
median="2x2-1-1"
|
|
|
|
while getopts p:s:g:q:M:e:xGmhSvPnt option ; do
|
|
case "${option}"
|
|
in
|
|
p) ((nopt+=2)) ;
|
|
prefix=${OPTARG}
|
|
;;
|
|
s) ((nopt+=2)) ;
|
|
suffix=${OPTARG}
|
|
;;
|
|
g) ((nopt+=2)) ;
|
|
geom=${OPTARG}
|
|
;;
|
|
q) ((nopt+=2)) ;
|
|
qual=${OPTARG}
|
|
;;
|
|
t) ((nopt+=1)) ;
|
|
time=true;
|
|
;;
|
|
e) ((nopt+=2)) ;
|
|
EXT=${OPTARG}
|
|
;;
|
|
v) ((nopt+=1)) ;
|
|
verbose=true;
|
|
;;
|
|
P) ((nopt+=1)) ;
|
|
progress=0;
|
|
;;
|
|
x) ((nopt+=1)) ;
|
|
exif=true;
|
|
;;
|
|
S) ((nopt+=1)) ;
|
|
unset sampling;
|
|
;;
|
|
G) ((nopt+=1)) ;
|
|
gaussian=true;
|
|
;;
|
|
n) ((nopt+=1)) ;
|
|
nochange=true;
|
|
;;
|
|
m) ((nopt+=1)) ;
|
|
unset median
|
|
;;
|
|
M) ((nopt+=2)) ;
|
|
a=${OPTARG}
|
|
b=$((a/2))
|
|
median=$a\x$a-$b-$b
|
|
echo $median
|
|
unset a
|
|
unset b
|
|
;;
|
|
h | -help)
|
|
echo -e $usage ;
|
|
exit 0
|
|
;;
|
|
*)
|
|
echo -e $usage ;
|
|
echo "Unreconized option: ${option} ${OPTARG}"
|
|
exit 0
|
|
;;
|
|
esac
|
|
done
|
|
|
|
test $verbose == true && echo -e "Old file \t Old size \t Old res \t New res \t New size \t New file \t Compression"
|
|
|
|
for f in "${@:$nopt}" ; do
|
|
|
|
test "$verbose" == true && status="$f \t $(ls -lh $f | cut -d " " -f 5) \t $(identify "${f}" | rev | cut -d " " -f 7 | rev) \t " ; size=$(ls -l "${f}" | cut -d " " -f 5)
|
|
|
|
name="$(echo $f | rev | cut -d '.' -f 2- | rev)"
|
|
ext="${EXT:-$(echo $f | rev | cut -d '.' -f 1 | rev)}"
|
|
|
|
if test "$time" ; then
|
|
tmp="$(exif -m -t "DateTime" "${f}")" && name="$(echo ${tmp} | tr ": " "-_" | head -n 1)" || echo "$f : no DateTime found in exif data. Using filename instead."
|
|
tmp="$(echo "${f}" | grep -E "([[:digit:]]{4}).?([[:digit:]]{2}).?([[:digit:]]{2}).?([[:digit:]]{2}).?([[:digit:]]{2}).?([[:digit:]]{2})" | sed -e "s/[^0-9]//g" | sed -E "s/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})/\1-\2-\3_\4-\5-\6/")"
|
|
test "${tmp}" && name="${tmp}"
|
|
fi
|
|
|
|
nf="${prefix}${name}${suffix}.${ext}"
|
|
|
|
while test -e $nf ; do nf="${prefix}${name}${suffix}"_$(((++i)))."${ext}" ; done
|
|
unset i
|
|
|
|
((insize+=$(ls -l "${f}" | cut -d " " -f 5)))
|
|
|
|
cmd="convert -auto-orient -define jpeg:dct-method=float $sampling ${median:+-median $median} -interlace Plane ${gaussian:+-gaussian-blur 0.1} -colorspace RGB -resize ${geom} -quality ${qual} ${f} ${nf}"
|
|
test "$exif" = true && cmd="$cmd ; exif --remove --no-fixup -o ${nf} ${nf} > /dev/null"
|
|
|
|
if test "${nochange}" ; then
|
|
echo "${cmd}"
|
|
else
|
|
$cmd
|
|
|
|
((outsize+=$(ls -l "${nf}" | cut -d " " -f 5)))
|
|
test "$verbose" == true && echo -e $status"$(identify "$nf" | rev | cut -d " " -f 7 | rev) \t $(ls -lh "$nf" | cut -d " " -f 5) \t "$nf" \t $((100*$(ls -l "$nf" | cut -d " " -f 5)/$size))% "
|
|
fi
|
|
|
|
test "$progress" && echo "$(((++progress * 100 / $#)))"
|
|
done
|
|
|
|
test $verbose == true && echo -e "Average compression : $((100*outsize/insize))%"
|