|
#!/bin/sh |
|
# https://gist.github.com/kpym/dc74bdc78a192add4ca4b3853fb3473d |
|
|
|
# Convert PDF to black and white CCITT Groupe 4 using ImageMagisk |
|
# and add blure filter with bbe or sed (if available). |
|
|
|
# ============== SET PARAMETERS ===================================== |
|
|
|
# convert arguments to variables |
|
while [ -n "$1" ]; do |
|
key="$1" |
|
shift # past argument |
|
case $key in |
|
-r|--resolution) |
|
resolution="$1" |
|
shift # past value |
|
;; |
|
-t|--threshold) |
|
thershold="$1" |
|
shift # past value |
|
;; |
|
*.pdf) # the pdf |
|
base="${key%.pdf}" # The pdf name without the extension .pdf |
|
;; |
|
*) # unknown option |
|
echo "Error: Unknown parameter $key" |
|
base="" |
|
esac |
|
done |
|
|
|
# Print the usage message (if needed) |
|
if [ -z "$base" ]; then |
|
echo "Usage: $0 file.pdf [options]" |
|
echo "Result: file_bw.pdf" |
|
echo "Options:" |
|
echo " -r, --resolution <int> : the resolution in dpi [default 200]" |
|
echo " -t, --thershold <0-100> : the black/white thershold in percent [default 77]" |
|
exit 1 |
|
fi |
|
|
|
# Check for ImageMagisk = magick on Windows, convert on Mac/Linux |
|
if command -v magick &> /dev/null |
|
then |
|
im=magick |
|
echo "Use magick (probably on Windows)" |
|
else |
|
if command -v convert &> /dev/null |
|
then |
|
im=convert |
|
echo "Use convert (hope is not Windows)" |
|
else |
|
echo "Error: can'n find ImageMagick (magick or convert)." |
|
exit 1 |
|
fi |
|
fi |
|
|
|
# Set the output resolution in dpi |
|
if [ -z "$resolution" ]; then |
|
resolution=200 |
|
fi |
|
echo "Resolution set to ${resolution}dpi." |
|
|
|
# Set the black and white thershold |
|
if [ -z "$thershold" ]; then |
|
thershold=77 |
|
fi |
|
echo "Thersnold set to ${thershold}%." |
|
|
|
# ===================== CONVERT ===================================== |
|
|
|
# Do the actual conversion |
|
echo "Converting ${base}.pdf to ccitt black and white pdf." |
|
$im -density $resolution "${base}.pdf" -threshold "${thershold}%" -type bilevel -compress Group4 "${base}_temp.pdf" |
|
|
|
# ==================== ADD BLUR ===================================== |
|
|
|
# Use bbe or sed (if present) to add interpolation option to CCITT strames |
|
if command -v bbe &> /dev/null; then |
|
echo "Add interpolation (blur) using bbe : ${base}_temp.pdf -> ${base}_bw.pdf" |
|
bbe -b "/BitsPerComponent 1/:18" -e "A /Interpolate\32true" "${base}_temp.pdf" -o "${base}_bw.pdf" |
|
elif command -v sed &> /dev/null; then |
|
echo "Add interpolation (blur) using sed : ${base}_temp.pdf -> ${base}_bw.pdf" |
|
sed -b -e 's;/BitsPerComponent 1;/BitsPerComponent 1/Interpolate true;' "${base}_temp.pdf" > "${base}_bw.pdf" |
|
else |
|
echo "Can't find bbe nor sed. The images will look very sharp." |
|
echo "Move: ${base}_temp.pdf -> ${base}_bw.pdf" |
|
mv "${base}_temp.pdf" "${base}_bw.pdf" |
|
fi |
|
|
|
# ====================== CLEAR ====================================== |
|
|
|
# Clear temporary files |
|
if [ -f "${base}_temp.pdf" ]; then |
|
echo delete temp pdf |
|
rm "${base}_temp.pdf" |
|
fi |