Comment by distances
4 years ago
I have a script for the same purpose too, but I prefer a black-and-white 1-bit palette for that fax look. Here's my version -- note that it uses graphicsmagick, img2pdf, optipng, and pdftk. Also enforces A4 so some of you may want to change that. For fun it's doing the page processing in parallel to speed up a bit with large documents.
#!/bin/bash
# Adds a bad scanning effect to PDF files.
if [ $# -ne 2 ]; then
echo 1>&2 "Usage: $0 input.pdf output.pdf"
exit 3
fi
convertPage() {
# PDF filename in first parameter, page in second
file=$1
page=$(($2-1))
png=$(printf "pdf2scan-page-%05d.png" $2)
# Convert PDF page to black and white PNG
gm convert -density 300 "$file"[$page] +dither -rotate 0.35 +noise Gaussian -type bilevel -fill white -fuzz 90% -colors 2 $png
# Optimize PNG
optipng -silent $png
}
export -f convertPage
# Read number of pages
pages=$(pdftk "$1" dump_data | grep NumberOfPages | sed 's/[^0-9]*//')
# Loop through pages and convert in parallel
for i in $(seq 1 $pages)
do
echo "$1":::$i
done | parallel --eta --colsep ':::' convertPage {1} {2}
# Create PDF from PNGs
img2pdf -o "$2" --producer "" --pagesize A4 pdf2scan-page-*.png
# Remove temporary files
rm pdf2scan-page*
For a cleaner 1-bit look without noise and rotation, use "gm convert -density 300 "$file"[$page] +dither -colors 2 -type bilevel -fill white -fuzz 40% $png".
The 1-bit palette is a good touch. Making it use parallel(1) is a great and easy optimization. Nice!