0

Simulate a scanned PDF with ImageMagick

 1 year ago
source link: https://gist.github.com/scf37/39e2724e7e091ed2a9bc75532244a832
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Simulate a scanned PDF with ImageMagick

  1. Download and install Ghostscript and ImageMagick + Convert Module
  2. Execute the following command in the console (change the filename)
$ convert -density 200 INPUT.pdf -rotate 0.3 +noise Multiplicative -format pdf  -quality 85 -compress JPEG -colorspace gray OUTPUT.pdf

Description of the options

Batch-Scripts

Python 3

#!/usr/bin/env python3
import os
import sys
import subprocess

'''
If you want use Drag and Drop, add this to the windows registry (or include into a .reg file and execute the file):

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Python.File\shellex\DropHandler]
@="{86C86720-42A0-1069-A2E8-08002B30309D}"
'''

# Important for Drag and Drop! Change current work directory to the directory of the script
os.chdir(os.path.abspath( os.path.dirname(os.path.realpath(__file__))))

default_input_dir = os.path.abspath('input')
output_dir = os.path.abspath('output')

def process(input_filename, output_filename):
    if not os.path.isdir(output_dir):
        os.makedirs(output_dir)

    s = 'convert -density 200 "%s" -rotate 0.3 +noise Multiplicative -format pdf -quality 85 -compress JPEG -colorspace gray "%s"' % (input_filename, output_filename)
    err, out = subprocess.Popen(s, stdout=subprocess.PIPE, shell=True).communicate()
    if err:
        sys.stderr.write(err)
        sys.stderr.flush()
    elif out:
        if sys.stdout.isatty():
            print(out)

    sys.stdout.write(output_filename+"\n")
    sys.stdout.flush()

def walkDir(dir):
    for root, dirs, files in os.walk(dir):
        for file in filter(lambda x: x.endswith(".pdf"), files):
            process(os.path.join(root, file), os.path.join(output_dir, file))

def main():
    if sys.stdin.isatty() and len(sys.argv) == 1:
        walkDir(default_input_dir)
    else:
        files = sys.argv[1:]
        if sys.stdin.isatty() is False:
            files += [line.rstrip('\n') for line in sys.stdin.readlines()]

        for x in files:
            x = os.path.abspath(x)
            if os.path.isdir(x):
                walkDir(x)
            elif os.path.isfile(x) and x.endswith(".pdf"):
                process(x, os.path.join(output_dir, os.path.basename(x)))

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        pass
    except Exception as e:
        sys.stderr.write(e)
        sys.stderr.flush()

PowerShell

$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition

$input_directory = $scriptPath + "\Input\"
$output_directory = $scriptPath + "\Output\"

New-Item -ItemType Directory -Force -Path $output_directory

$files = Get-ChildItem $input_directory -Filter *.pdf

ForEach ($file in $files) {
    "Process: " + $file.Name 
    $command = "convert -density 200 '" + $input_directory + $file.Name  + "' -rotate 0.3 +noise Multiplicative -format pdf -quality 85 -compress JPEG -colorspace gray '" + $output_directory + $file.Name + "'"
    Invoke-Expression -command $command
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK