Skip to content

Instantly share code, notes, and snippets.

@kpym
Last active February 1, 2021 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kpym/dc74bdc78a192add4ca4b3853fb3473d to your computer and use it in GitHub Desktop.
Save kpym/dc74bdc78a192add4ca4b3853fb3473d to your computer and use it in GitHub Desktop.
Convert PDF to black and white CCITT Groupe 4 one. Use ImageMagisk and bbe or sed (if available).

Description

This script needs the following tools :

  • ImageMagick, convert (Mac/Linux) or magick (Windows), to convert all .png pages to black and white and compress them with CCITT Groupe 4 compression.
  • Use bbe or sed (if present) to add interpolation option to all CCITT streams. This makes the pages more pleasing to the eye (less "pixelated").

Usage

> ./pdf2bw.sh
Usage: pdf2bw.sh file.pdf [options]
Result: file_bw.pdf
Options:
  -r, --resolution <int> : the resolution in dpi [default 200]
  -t, --thershold <0-100> : the black/white thershold in percent [default 77]
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment