#!/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".It was a "government document simulator." What you would do is upload a nicely scanned document, and it'd give you back a mis-alighed, crappy quality "scan" of that document, with random blotches and other visual noise. You know, like regular government/FOIA-received documents.
I feel like this is halfway there, if not more (so thank you!), but that website was so authentic.
I don't know if it's even around, but it made me giggle, and I'd like to find it again. If not--great startup idea!
"-flatten" results in all PDF pages being rendered into a 1 page PDF output. If "-flatten" is removed, I get a multi-page PDF output as expected. Thoughts?
EDIT: "-flatten" does what it is supposed to. Delete if operating on multipage PDF.
Edit: haha! The "-flatten" needs to be replaced with "-alpha flatten". This way, multi-page documents are still handled correctly, and alpha transparency is also handled correctly. I just tried on this sample file with transparent images: https://tcpdf.org/files/examples/example_042.pdf
> convert: UnrecognizedAlphaChannelOption `flatten' @ error/convert.c/ConvertImageCommand/673.
"+noise Random -fill white -colorize 95%" to add some noise to the image
"-distort ScaleRotateTranslate '$x,$y $angle'" to randomly shift horizontally and vertically the document, and randomly rotate it slightly
"-density 150" for a low-ish resolution so it better hides the fact the PDF wasn't really scanned
"-colorspace Gray" to make it black & white
"-quality 60" to increase JPG compression and somewhat reduce picture quality