Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Make a pdf look scanned using ImageMagick
# use ImageMagick convert
# the order is important. the density argument applies to input.pdf and resize and rotate to output.pdf
convert -density 90 input.pdf -rotate 0.5 -attenuate 0.2 +noise Multiplicative -colorspace Gray output.pdf
@sherbang

This comment has been minimized.

Copy link

@sherbang sherbang commented Aug 29, 2018

Thanks, this got me started in the right direction. I found that these settings made it look more convincing:
convert -density 90 input.pdf -rotate 0.5 -attenuate 0.2 +noise Multiplicative -colorspace Gray output.pdf

@andyrbell

This comment has been minimized.

Copy link
Owner Author

@andyrbell andyrbell commented Sep 9, 2018

@sherbang very nice, looks much better. Have updated. Thanks!

@muellermartin

This comment has been minimized.

Copy link

@muellermartin muellermartin commented Jul 10, 2019

Thank you very much for sharing! I improved it even further (randomized rotation direction and amount and added more noise):

convert -density 150 input.pdf -rotate "$([ $((RANDOM % 2)) -eq 1 ] && echo -)0.$(($RANDOM % 4 + 5))" -attenuate 0.4 +noise Multiplicative -flatten -attenuate 0.03 +noise Multiplicative -sharpen 0x1.0 -colorspace Gray output.pdf
@zbot473

This comment has been minimized.

Copy link

@zbot473 zbot473 commented Aug 27, 2019

How would I do multi-page PDFs?

@decadenza

This comment has been minimized.

Copy link

@decadenza decadenza commented Nov 2, 2019

up

@coryodaniel

This comment has been minimized.

Copy link

@coryodaniel coryodaniel commented Mar 10, 2020

Take out the -flatten for multipage @zbot473

convert -density 150 input.pdf -rotate "$([ $((RANDOM % 2)) -eq 1 ] && echo -)0.$(($RANDOM % 4 + 5))" -attenuate 0.4 +noise Multiplicative -attenuate 0.03 +noise Multiplicative -sharpen 0x1.0 -colorspace Gray output.pdf
@zbot473

This comment has been minimized.

Copy link

@zbot473 zbot473 commented Apr 1, 2020

Thanks.

@klaus-schuster

This comment has been minimized.

Copy link

@klaus-schuster klaus-schuster commented Apr 4, 2020

Works perfect! Thanks guys! - i added a macox automator quick action.. in case somebody can use it ...

saves the files in the same folder with _scanned attached to the fileaname

"Pass input" has to be set -as arguments-

heres the script


export PATH=/usr/local/bin:$PATH
for f in "$@"
do
	base=${f%.pdf}
	base=$base"_scanned.pdf"
	convert -density 150 "$f" -attenuate 0.4 +noise Multiplicative -rotate "$([ $((RANDOM % 2)) -eq 1 ] && echo -)0.$(($RANDOM % 4 + 5))" -attenuate 0.03 +noise Multiplicative -sharpen 0x1.0 -colorspace Gray "$base"

done
@bobishh

This comment has been minimized.

Copy link

@bobishh bobishh commented Apr 9, 2020

Thanks everyone indeed!

Also make sure you have ghostscript installed (brew install gs) alongside with ImageMagick if you're on MacOS
Scripts were giving me cryptic

convert: no images defined `output.pdf' @ error/convert.c/ConvertImageCommand/3273.

without ghostscript

@rgjr

This comment has been minimized.

Copy link

@rgjr rgjr commented Apr 29, 2020

convert -density 150 input.pdf -rotate "$([ $((RANDOM % 2)) -eq 1 ] && echo -)0.$(($RANDOM % 4 + 5))" -attenuate 0.4 +noise Multiplicative -attenuate 0.03 +noise Multiplicative -sharpen 0x1.0 -colorspace Gray output.pdf

Wow this looks great, thanks!

@m3nu

This comment has been minimized.

Copy link

@m3nu m3nu commented May 8, 2020

Very useful. Added as function:

function pdf.like_scanned () {
        OUT=$(basename "$1" .pdf)
	convert -density 150 "$1" -rotate "$([ $((RANDOM % 2)) -eq 1 ] && echo -)0.$(($RANDOM % 4 + 5))" \
                -attenuate 0.4 +noise Multiplicative -attenuate 0.03 +noise Multiplicative -sharpen 0x1.0 \
                -colorspace Gray "$OUT"_scanned.pdf
}
@baicunko

This comment has been minimized.

Copy link

@baicunko baicunko commented May 11, 2020

I just developed www.scanyourpdf.com for everyone to use. Code is open source if you'd like to contribute!

@kidsil

This comment has been minimized.

Copy link

@kidsil kidsil commented May 13, 2020

convert -density 130 input.pdf -rotate 0.33 -attenuate 0.15 +noise Multiplicative -colorspace Gray output.pdf

Closest to a modern scanner in my opinion.

@shanecp

This comment has been minimized.

Copy link

@shanecp shanecp commented Jun 7, 2020

Sometimes you'll have to replace a few pages with real scanned pages. Eg: Replace the signature page.

The flow will be:

  • Convert input as a scanned PDF.
  • Split the sections that should be replaced with a real scan.
  • Merge everything back to the output.

You'll need qpdf and img2pdf installed.

convert -density 130 input.pdf -rotate -0.33 -attenuate 0.15 +noise Multiplicative -colorspace Gray output.pdf
qpdf --empty --pages output.pdf 1-5 -- output_1.pdf
img2pdf --pagesize A4 --auto-orient signed.jpg -o output_2.pdf
qpdf --empty --pages output.pdf 7 -- output_3.pdf
qpdf --empty --pages output_*.pdf -- final_scan.pdf
@turkeyphant

This comment has been minimized.

Copy link

@turkeyphant turkeyphant commented Jun 11, 2020

I get the following:

C:\Program Files\ImageMagick-7.0.10-Q16-HDRI>magick convert -density 150 input.pdf -rotate "$([ $((RANDOM % 2)) -eq 1 ] && echo -)0.$(($RANDOM % 4 + 5))" -attenuate 0.4 +noise Multiplicative -attenuate 0.03 +noise Multiplicative -sharpen 0x1.0 -colorspace Gray output.pdf
convert: FailedToExecuteCommand `"gswin32c.exe" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r150x150"  "-sOutputFile=C:/Users/TURKEY~1/AppData/Local/Temp/magick-9420wtSmlrXSBcfh%d" "-fC:/Users/TURKEY~1/AppData/Local/Temp/magick-9420IJ2RKxHzcTQf" "-fC:/Users/TURKEY~1/AppData/Local/Temp/magick-9420t7VINjcK97Pq"' (The system cannot find the file specified.
) @ error/delegate.c/ExternalDelegateCommand/475.
convert: PDFDelegateFailed `The system cannot find the file specified.
' @ error/pdf.c/ReadPDFImage/662.
convert: invalid argument for option '-rotate': $([ $((RANDOM % 2)) -eq 1 ] && echo -)0.$(($RANDOM % 4 + 5)) @ error/convert.c/ConvertImageCommand/2643.
@muellermartin

This comment has been minimized.

Copy link

@muellermartin muellermartin commented Jun 11, 2020

@turkeyphant: You seem to use Windows. Most commands here use features from UNIX shells like Bash (e.g. command substitution via $(), the $RANDOM variable, arithmetic expressions or conditionals). These features are not available in the default Windows command line, therefore you need to find another way (e.g. remove UNIX shell features from the command or use a UNIX shell like Cygwin, Git shell or WSL under Windows).

@turkeyphant

This comment has been minimized.

Copy link

@turkeyphant turkeyphant commented Jun 12, 2020

@muellermartin: oops good point. However, I'm still having issues on a OS X machine when running brew install imagemagick. It seems to be either an issue with curl (I don't know how to sub in a different version) or a 301 redirect at kernel.org:

curl: (60) SSL certificate problem: Invalid certificate chain
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.
Error: Failed to download resource "gnu-getopt"
Download failed: https://www.kernel.org/pub/linux/utils/util-linux/v2.35/util-linux-2.35.2.tar.xz

@muellermartin

This comment has been minimized.

Copy link

@muellermartin muellermartin commented Jun 12, 2020

@turkeyphant Nice that you have macOS at hand – these ImageMagick commands should work there :) You seem to have bad luck due to your issue with Homebrew, though. The error seems to indicate that curl (probably used by brew to download the dependencies for ImageMagick) can't validate the SSL certificate (the redirect is likely not an issue). As the certificate for www.kernel.org seems to be valid from my point, there is likey some issue with the certificate bundle used by your curl. This is odd, as the pre-installed version of curl should use the system certificates and therefore it should work. Maybe you have "overwritten" the curl command via Homebrew (which is not recommended). You can check that by using which curl which should output /usr/bin/curl. If the output is something like /usr/local/bin/curl or /usr/local/opt/curl/bin/curl then you might have linked the version from Homebrew (or other tools). With Homebrew you can try brew unlink curl to undo this.

@turkeyphant

This comment has been minimized.

Copy link

@turkeyphant turkeyphant commented Jun 12, 2020

I haven't messed with curl.

$ which curl
/usr/bin/curl

Any other workaround for this download?

@muellermartin

This comment has been minimized.

Copy link

@muellermartin muellermartin commented Jun 12, 2020

@turkeyphant: Hm, I wonder why Homebrew tries to install gnu-getopt from the sources instead of using a keg file. Maybe you could try to install gnu-getopt explicitly to work around this issue: brew install gnu-getopt

@turkeyphant

This comment has been minimized.

Copy link

@turkeyphant turkeyphant commented Jun 12, 2020

No dice I'm afraid. Still get Error: Failed to download resource "gnu-getopt" Download failed: https://www.kernel.org/pub/linux/utils/util-linux/v2.35/util-linux-2.35.2.tar.xz

Given I'm able to download the file manually there must be a workaround? Any way to tell brew not to make curl to use -k? Or use wget --no-check-certificate instead?

@turkeyphant

This comment has been minimized.

Copy link

@turkeyphant turkeyphant commented Jun 12, 2020

Seem to have solved it (slowing making as I write) with Homebrew/legacy-homebrew#6103 (comment) for each and every invalid cert.

Do think there must be a way to update my machine's certs so that curl can work correctly though.

@muellermartin

This comment has been minimized.

Copy link

@muellermartin muellermartin commented Jun 12, 2020

@turkeyphant: Well, if these SSL errors are not only related to curl then something is really off. Sometimes an utterly wrong system time causes such errors (because the certificates seem to be expired/not valid yet) or you're in a shitty corporate network that uses some kind of HTTPS-Interception and thus breaks security or you're the victim of a MITM attack.

@turkeyphant

This comment has been minimized.

Copy link

@turkeyphant turkeyphant commented Jun 12, 2020

It's seems to be a common macos issue to be honest. System time is correct, there is no vpn or other network issues and I'm fairly certain there's no mitm going on (have tested various Internet connections for example and other macos machines). It's 10.11 and the certificates might just be out of date?

@DavidWuthier

This comment has been minimized.

Copy link

@DavidWuthier DavidWuthier commented Oct 5, 2020

Nice! The other day, I had 19 pages to sign with unique signatures. First, I used xournal on Ubuntu 20.04 with a stylus, and then I ran the following script:

#!/usr/bin/env bash

# Dependencies
sudo apt install pdftk imagemagick -y

# Output folder
mkdir -p output

# Keep pages in the right order
for i in {1..19}; do
  if (( $i < 10 )); then
    j=0$i
  else
    j=$i
  fi

  pdftk input.pdf cat $i output output/$j.pdf
  convert -density 200 -trim -flatten -quality 80 -attenuate 0.15 +noise Multiplicative -rotate 0.01 output/$j.pdf output/$j.jpg
  convert output/$j.jpg output/$j.pdf
  rm output/$j.jpg
done

pdftk output/* cat output result.pdf

The conversion to .jpg prevents the file from bloating.

@michaelrkn

This comment has been minimized.

Copy link

@michaelrkn michaelrkn commented Nov 3, 2020

The +noise Multiplicative argument created a dappled background behind where I had text but not in other places. Using Gaussian, Laplacian, or Uniform instead of Multiplicative produced better results for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.