-
-
Save Pezmc/38017cb03daccb17d3835280c568dc0f to your computer and use it in GitHub Desktop.
Make a pdf look scanned with macOS automator as a quick action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script takes a PDF or list of PDFs and outputs a file(s) | |
# named <file>_scanned.pdf that looks like it has been scanned | |
# | |
# Requires imagemagic and popper to be installed (brew install imagemagick poppler) | |
# | |
# Accepts: a list of files | |
# Usage: ./<scriptfilename> pdf1.pdf pdf2.pdf | |
# | |
# To use as a macOS automator quick action you need to: | |
# - Create a new quick action that receives files or folders | |
# - Add a shell script | |
# - Paste in this script | |
set -e | |
export PATH=/usr/local/bin:$PATH | |
for file in "$@"; do | |
base=${file%.pdf} | |
base=$base"_scanned.pdf" | |
# Split PDF into pages | |
echo "Splitting $base into separate pages" | |
pdfseparate "$file" /tmp/fake-scan-split-%04d.pdf | |
# Loop over the pages | |
for splitFile in /tmp/fake-scan-split-*.pdf; do | |
splitFileBase=${splitFile%.pdf} | |
splitFileScanned=$splitFileBase"_scanned.pdf" | |
# Slightly rotate page, add a bit of noise and output a flat pdf | |
convert -density 130 -trim -flatten "$splitFile" -attenuate 0.2 +noise Gaussian -rotate "$([ $((RANDOM % 2)) -eq 1 ] && echo -)0.$(($RANDOM % 8 + 1))" "$splitFileScanned" | |
echo "Output page $splitFileBase to $splitFileScanned" | |
done | |
# Combine the PDFs, add noise across the entire document, apply sharpening, convert to b&w, soften the blacks slightly | |
convert -density 130 $(ls -rt /tmp/fake-scan-split-*_scanned.pdf) -attenuate 0.2 +noise Multiplicative -sharpen 0x1.0 -colorspace Gray +level 15%,100% "$base" | |
echo "PDF re-combined to $base" | |
# Remove all the temporary PDFs | |
echo "Cleaning up" | |
rm /tmp/fake-scan-split-*.pdf | |
done |
Hi @Pezmc! I really liked your snippet, but I missed a false real signal of a scanned document, the shadows between the margin of scanner and the document. I can produce this with the original scanner.sh script, but with your changes the shadow doesn't show.
Nice
I had to add some absolute paths to the binaries to made it work with homebrew. I’ve also added a small shadow to the rotated pages:
#!/bin/bash
# This script takes a PDF or list of PDFs and outputs a file(s)
# named <file>_scanned.pdf that looks like it has been scanned
#
# Requires imagemagic and popper to be installed (brew install imagemagick poppler)
#
# Accepts: a list of files
# Usage: ./<scriptfilename> pdf1.pdf pdf2.pdf
#
# To use as a macOS automator quick action you need to:
# - Create a new quick action that receives files or folders
# - Add a shell script
# - Paste in this script
set -e
export PATH=/usr/local/bin:$PATH
for file in "$@"; do
base=${file%.pdf}
base=$base"_scanned.pdf"
# Remove all temp pdfs before
# rm -f /tmp/fake-scan-split-*.pdf
# Split PDF into pages
echo "Splitting $base into separate pages"
/opt/homebrew/bin/pdfseparate "$file" /tmp/fake-scan-split-%04d.pdf
# Loop over the pages
for splitFile in /tmp/fake-scan-split-*.pdf; do
splitFileBase=${splitFile%.pdf}
splitFileScanned=$splitFileBase"_scanned.pdf"
# Slightly rotate page, add a bit of noise and output a flat pdf
/opt/homebrew/bin/convert -density 130 -trim -flatten "$splitFile" -attenuate 0.2 +noise Gaussian -rotate "$([ $((RANDOM % 2)) -eq 1 ] && echo -)0.$(($RANDOM % 8 + 1))" \( +clone -background black -shadow 30x5+5+5 \) +swap -background white -layers merge +repage "$splitFileScanned"
echo "Output page $splitFileBase to $splitFileScanned"
done
# Combine the PDFs, add noise across the entire document, apply sharpening, convert to b&w, soften the blacks slightly
/opt/homebrew/bin/convert -density 130 $(ls -rt /tmp/fake-scan-split-*_scanned.pdf) -attenuate 0.2 +noise Multiplicative -sharpen 0x1.0 -colorspace Gray +level 15%,100% "$base"
# /opt/homebrew/bin/convert rose: ( +clone -background navy -shadow 80x3+5+5 ) -density 130 $(ls -rt /tmp/fake-scan-split-*_scanned.pdf) -attenuate 0.2 +noise Multiplicative -sharpen 0x1.0 -colorspace Gray +level 15%,100% "$base"
echo "PDF re-combined to $base"
# Remove all the temporary PDFs
echo "Cleaning up"
rm /tmp/fake-scan-split-*.pdf
done
Thank you all so much for your work!
Bravo!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks! awesome :)