Skip to content

Instantly share code, notes, and snippets.

@fieri
Forked from scf37/simulate_scanned_PDF.md
Created April 11, 2022 05:13
Show Gist options
  • Save fieri/02d40eaaa733976a2d53f18bea5033c0 to your computer and use it in GitHub Desktop.
Save fieri/02d40eaaa733976a2d53f18bea5033c0 to your computer and use it in GitHub Desktop.

Revisions

  1. Benny Samir H revised this gist Nov 24, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # Simulate a scanned PDF with ImageMagick
    1. Download and install [ImageMagick + Convert Module](https://www.imagemagick.org/script/binary-releases.php)
    1. Download and install [Ghostscript](http://ghostscript.com/download/gsdnld.html) and [ImageMagick + Convert Module](https://www.imagemagick.org/script/binary-releases.php)
    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
  2. Benny Samir H revised this gist Nov 23, 2016. 1 changed file with 0 additions and 94 deletions.
    94 changes: 0 additions & 94 deletions simulate_scanned_pdf-multithreading.py
    Original file line number Diff line number Diff line change
    @@ -1,94 +0,0 @@
    #!/usr/bin/env python3
    import os
    import sys
    import subprocess
    import threading, queue
    from datetime import datetime

    '''
    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')

    class converter(threading.Thread):
    workqueue = queue.Queue()
    lock = threading.Lock()
    inited = False


    def __init__(self):
    threading.Thread.__init__(self)

    @staticmethod
    def init():
    converter.inited = True
    for thread in [converter() for _ in range(os.cpu_count())]:
    thread.setDaemon(True)
    thread.start()

    @staticmethod
    def wait():
    return converter.workqueue.join()

    @staticmethod
    def put(input_filename, output_filename):
    if converter.inited is False:
    converter.init()
    converter.workqueue.put((input_filename, output_filename))

    def run(self):
    while True:
    input_filename, output_filename = converter.workqueue.get()

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

    converter.lock.acquire()
    print(output_filename)
    converter.lock.release()

    converter.workqueue.task_done()

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

    def main():
    if not os.path.isdir(output_dir):
    os.makedirs(output_dir)

    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"):
    converter.put(x, os.path.join(output_dir, os.path.basename(x)))

    converter.wait()

    if __name__ == "__main__":
    try:
    main()
    except KeyboardInterrupt:
    pass
  3. Benny Samir H revised this gist Nov 23, 2016. 1 changed file with 94 additions and 0 deletions.
    94 changes: 94 additions & 0 deletions simulate_scanned_pdf-multithreading.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    #!/usr/bin/env python3
    import os
    import sys
    import subprocess
    import threading, queue
    from datetime import datetime

    '''
    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')

    class converter(threading.Thread):
    workqueue = queue.Queue()
    lock = threading.Lock()
    inited = False


    def __init__(self):
    threading.Thread.__init__(self)

    @staticmethod
    def init():
    converter.inited = True
    for thread in [converter() for _ in range(os.cpu_count())]:
    thread.setDaemon(True)
    thread.start()

    @staticmethod
    def wait():
    return converter.workqueue.join()

    @staticmethod
    def put(input_filename, output_filename):
    if converter.inited is False:
    converter.init()
    converter.workqueue.put((input_filename, output_filename))

    def run(self):
    while True:
    input_filename, output_filename = converter.workqueue.get()

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

    converter.lock.acquire()
    print(output_filename)
    converter.lock.release()

    converter.workqueue.task_done()

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

    def main():
    if not os.path.isdir(output_dir):
    os.makedirs(output_dir)

    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"):
    converter.put(x, os.path.join(output_dir, os.path.basename(x)))

    converter.wait()

    if __name__ == "__main__":
    try:
    main()
    except KeyboardInterrupt:
    pass
  4. Benny Samir H revised this gist Nov 22, 2016. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -34,8 +34,8 @@ Windows Registry Editor Version 5.00
    # 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')+"\\"
    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):
    @@ -56,7 +56,7 @@ def process(input_filename, output_filename):
    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), str(output_dir + file))
    process(os.path.join(root, file), os.path.join(output_dir, file))

    def main():
    if sys.stdin.isatty() and len(sys.argv) == 1:
    @@ -71,7 +71,7 @@ def main():
    if os.path.isdir(x):
    walkDir(x)
    elif os.path.isfile(x) and x.endswith(".pdf"):
    process(x, output_dir + os.path.basename(x))
    process(x, os.path.join(output_dir, os.path.basename(x)))

    if __name__ == "__main__":
    try:
  5. Benny Samir H revised this gist Nov 22, 2016. 1 changed file with 21 additions and 32 deletions.
    53 changes: 21 additions & 32 deletions simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -37,61 +37,50 @@ 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 printProgress (iteration, total, prefix = '', suffix = '', decimals = 1, barLength = 100):
    formatStr = "{0:." + str(decimals) + "f}"
    percent = formatStr.format(100 * (iteration / float(total)))
    filledLength = int(round(barLength * iteration / float(total)))
    bar = '' * filledLength + '-' * (barLength - filledLength)
    sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percent, '%', suffix)),
    if iteration == total:
    sys.stdout.write('\n')
    sys.stdout.flush()

    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:
    print(err)
    sys.stderr.write(err)
    sys.stderr.flush()
    elif out:
    print(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):
    files = list(filter(lambda x: x.endswith(".pdf"), files))
    i = 0;
    printProgress(i, len(files), prefix = 'Progress:', suffix = 'Complete', barLength = 50)
    for file in files:
    for file in filter(lambda x: x.endswith(".pdf"), files):
    process(os.path.join(root, file), str(output_dir + file))
    i += 1
    printProgress(i, len(files), prefix = 'Progress:', suffix = 'Complete', barLength = 50)

    def main():
    if len(sys.argv) == 1:
    print("Process default Input-Directory: "+default_input_dir)
    if sys.stdin.isatty() and len(sys.argv) == 1:
    walkDir(default_input_dir)
    else:
    for arg in sys.argv[1:]:
    arg = os.path.abspath(arg)
    if os.path.isdir(arg):
    print("Processing Directory: "+arg)
    walkDir(arg)
    elif os.path.isfile(arg) and arg.endswith(".pdf"):
    print("Processing single File: "+arg)
    process(arg, output_dir + os.path.basename(arg))
    else:
    print("Unknown Path-Type!")
    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, output_dir + os.path.basename(x))

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

    ### PowerShell
  6. Benny Samir H revised this gist Nov 21, 2016. 1 changed file with 0 additions and 129 deletions.
    129 changes: 0 additions & 129 deletions simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -21,135 +21,6 @@ $ convert -density 200 INPUT.pdf -rotate 0.3 +noise Multiplicative -format pdf
    import os
    import sys
    import subprocess
    import win32api

    '''
    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 printProgress (iteration, total, prefix = '', suffix = '', decimals = 1, barLength = 100):
    formatStr = "{0:." + str(decimals) + "f}"
    percent = formatStr.format(100 * (iteration / float(total)))
    filledLength = int(round(barLength * iteration / float(total)))
    bar = '' * filledLength + '-' * (barLength - filledLength)
    sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percent, '%', suffix)),
    if iteration == total:
    sys.stdout.write('\n')
    sys.stdout.flush()

    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)
    #!/usr/bin/env python3
    import os
    import sys
    import subprocess
    import win32api

    '''
    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 printProgress (iteration, total, prefix = '', suffix = '', decimals = 1, barLength = 100):
    formatStr = "{0:." + str(decimals) + "f}"
    percent = formatStr.format(100 * (iteration / float(total)))
    filledLength = int(round(barLength * iteration / float(total)))
    bar = '' * filledLength + '-' * (barLength - filledLength)
    sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percent, '%', suffix)),
    if iteration == total:
    sys.stdout.write('\n')
    sys.stdout.flush()

    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:
    print(err)
    elif out:
    print(out)

    def walkDir(dir):
    #!/usr/bin/env python3
    import os
    import sys
    import subprocess
    import win32api

    '''
    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 printProgress (iteration, total, prefix = '', suffix = '', decimals = 1, barLength = 100):
    formatStr = "{0:." + str(decimals) + "f}"
    percent = formatStr.format(100 * (iteration / float(total)))
    filledLength = int(round(barLength * iteration / float(total)))
    bar = '' * filledLength + '-' * (barLength - filledLength)
    sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percent, '%', suffix)),
    if iteration == total:
    sys.stdout.write('\n')
    sys.stdout.flush()

    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:
    print(err)
    elif out:
    print(out)

    def walkDir(dir):
    for root, dirs, files in os.walk(dir):
    files = list(filter(lambda x: x.endswith(".pdf"), files))
    i = 0;
    printProgress(i, len(files), prefix = 'Progress:', suffix = 'Complete', barLength = 50)
    for file in files:
    process(os.path.join(root, file), str(output_dir + file))
    #!/usr/bin/env python3
    import os
    import sys
    import subprocess
    import win32api

    '''
    If you want use Drag and Drop, add this to the windows registry (or include into a .reg file and execute the file):
  7. Benny Samir H revised this gist Nov 21, 2016. 1 changed file with 49 additions and 0 deletions.
    49 changes: 49 additions & 0 deletions simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -117,6 +117,55 @@ 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 printProgress (iteration, total, prefix = '', suffix = '', decimals = 1, barLength = 100):
    formatStr = "{0:." + str(decimals) + "f}"
    percent = formatStr.format(100 * (iteration / float(total)))
    filledLength = int(round(barLength * iteration / float(total)))
    bar = '' * filledLength + '-' * (barLength - filledLength)
    sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percent, '%', suffix)),
    if iteration == total:
    sys.stdout.write('\n')
    sys.stdout.flush()

    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:
    print(err)
    elif out:
    print(out)

    def walkDir(dir):
    for root, dirs, files in os.walk(dir):
    files = list(filter(lambda x: x.endswith(".pdf"), files))
    i = 0;
    printProgress(i, len(files), prefix = 'Progress:', suffix = 'Complete', barLength = 50)
    for file in files:
    process(os.path.join(root, file), str(output_dir + file))
    #!/usr/bin/env python3
    import os
    import sys
    import subprocess
    import win32api

    '''
    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 printProgress (iteration, total, prefix = '', suffix = '', decimals = 1, barLength = 100):
    formatStr = "{0:." + str(decimals) + "f}"
    percent = formatStr.format(100 * (iteration / float(total)))
  8. Benny Samir H revised this gist Nov 21, 2016. 1 changed file with 43 additions and 0 deletions.
    43 changes: 43 additions & 0 deletions simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -74,6 +74,49 @@ 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 printProgress (iteration, total, prefix = '', suffix = '', decimals = 1, barLength = 100):
    formatStr = "{0:." + str(decimals) + "f}"
    percent = formatStr.format(100 * (iteration / float(total)))
    filledLength = int(round(barLength * iteration / float(total)))
    bar = '' * filledLength + '-' * (barLength - filledLength)
    sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percent, '%', suffix)),
    if iteration == total:
    sys.stdout.write('\n')
    sys.stdout.flush()

    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:
    print(err)
    elif out:
    print(out)

    def walkDir(dir):
    #!/usr/bin/env python3
    import os
    import sys
    import subprocess
    import win32api

    '''
    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 printProgress (iteration, total, prefix = '', suffix = '', decimals = 1, barLength = 100):
    formatStr = "{0:." + str(decimals) + "f}"
    percent = formatStr.format(100 * (iteration / float(total)))
  9. Benny Samir H revised this gist Nov 21, 2016. 1 changed file with 38 additions and 3 deletions.
    41 changes: 38 additions & 3 deletions simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -38,6 +38,42 @@ 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 printProgress (iteration, total, prefix = '', suffix = '', decimals = 1, barLength = 100):
    formatStr = "{0:." + str(decimals) + "f}"
    percent = formatStr.format(100 * (iteration / float(total)))
    filledLength = int(round(barLength * iteration / float(total)))
    bar = '' * filledLength + '-' * (barLength - filledLength)
    sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percent, '%', suffix)),
    if iteration == total:
    sys.stdout.write('\n')
    sys.stdout.flush()

    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)
    #!/usr/bin/env python3
    import os
    import sys
    import subprocess
    import win32api

    '''
    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 printProgress (iteration, total, prefix = '', suffix = '', decimals = 1, barLength = 100):
    formatStr = "{0:." + str(decimals) + "f}"
    percent = formatStr.format(100 * (iteration / float(total)))
    @@ -79,7 +115,7 @@ def main():
    if os.path.isdir(arg):
    print("Processing Directory: "+arg)
    walkDir(arg)
    elif arg.endswith(".pdf"):
    elif os.path.isfile(arg) and arg.endswith(".pdf"):
    print("Processing single File: "+arg)
    process(arg, output_dir + os.path.basename(arg))
    else:
    @@ -92,8 +128,7 @@ if __name__ == "__main__":
    pass
    except Exception as e:
    print(e)
    finally:
    input("\nPress any key to exit...")
    input()
    ```

    ### PowerShell
  10. Benny Samir H revised this gist Nov 21, 2016. 1 changed file with 73 additions and 18 deletions.
    91 changes: 73 additions & 18 deletions simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -19,26 +19,81 @@ $ convert -density 200 INPUT.pdf -rotate 0.3 +noise Multiplicative -format pdf
    ```python
    #!/usr/bin/env python3
    import os
    import sys
    import subprocess
    import win32api

    input_dir = "Input/"
    output_dir = "Output/"

    if not os.path.exists(output_dir):
    os.makedirs(output_dir)
    for root, dirs, files in os.walk(input_dir):
    for file in files:
    if file.endswith(".pdf"):
    input_filename = os.path.join(root, file)
    print ("Process: " + input_filename)
    p = subprocess.Popen(
    'convert -density 200 '+input_filename+' -rotate 0.3 +noise Multiplicative -format pdf -quality 85 -compress JPEG -colorspace gray '+str(output_dir + file),
    stdout=subprocess.PIPE, shell=True)
    err, out = p.communicate()
    if err:
    print(err)
    elif out:
    print(out)
    '''
    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 printProgress (iteration, total, prefix = '', suffix = '', decimals = 1, barLength = 100):
    formatStr = "{0:." + str(decimals) + "f}"
    percent = formatStr.format(100 * (iteration / float(total)))
    filledLength = int(round(barLength * iteration / float(total)))
    bar = '' * filledLength + '-' * (barLength - filledLength)
    sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percent, '%', suffix)),
    if iteration == total:
    sys.stdout.write('\n')
    sys.stdout.flush()

    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:
    print(err)
    elif out:
    print(out)

    def walkDir(dir):
    for root, dirs, files in os.walk(dir):
    files = list(filter(lambda x: x.endswith(".pdf"), files))
    i = 0;
    printProgress(i, len(files), prefix = 'Progress:', suffix = 'Complete', barLength = 50)
    for file in files:
    process(os.path.join(root, file), str(output_dir + file))
    i += 1
    printProgress(i, len(files), prefix = 'Progress:', suffix = 'Complete', barLength = 50)

    def main():
    if len(sys.argv) == 1:
    print("Process default Input-Directory: "+default_input_dir)
    walkDir(default_input_dir)
    else:
    for arg in sys.argv[1:]:
    arg = os.path.abspath(arg)
    if os.path.isdir(arg):
    print("Processing Directory: "+arg)
    walkDir(arg)
    elif arg.endswith(".pdf"):
    print("Processing single File: "+arg)
    process(arg, output_dir + os.path.basename(arg))
    else:
    print("Unknown Path-Type!")

    if __name__ == "__main__":
    try:
    main()
    except KeyboardInterrupt:
    pass
    except Exception as e:
    print(e)
    finally:
    input("\nPress any key to exit...")
    ```

    ### PowerShell
  11. Benny Samir H revised this gist Nov 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ $ convert -density 200 INPUT.pdf -rotate 0.3 +noise Multiplicative -format pdf
    - **-compress:** set compress to jpeg (other Compress-Options: https://www.imagemagick.org/script/command-line-options.php#compress)
    - **-colorspace:** set color to gray

    ## Batch-Scripte
    ## Batch-Scripts
    ### Python 3
    ```python
    #!/usr/bin/env python3
  12. Benny Samir H revised this gist Nov 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Simulate a scanned PDF with ImageMagick
    1. Download and install [ImageMagick + Convert Module](https://www.imagemagick.org/script/binary-releases.php)
    2. Execute the following command in the console
    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
    ```
  13. Benny Samir H revised this gist Nov 18, 2016. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -39,4 +39,22 @@ for root, dirs, files in os.walk(input_dir):
    print(err)
    elif out:
    print(out)
    ```

    ### PowerShell
    ```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
    }
    ```
  14. Benny Samir H revised this gist Nov 18, 2016. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -15,9 +15,9 @@ $ convert -density 200 INPUT.pdf -rotate 0.3 +noise Multiplicative -format pdf
    - **-colorspace:** set color to gray

    ## Batch-Scripte
    ### Python (2.7)
    ### Python 3
    ```python
    from __future__ import print_function
    #!/usr/bin/env python3
    import os
    import subprocess

    @@ -32,5 +32,11 @@ for root, dirs, files in os.walk(input_dir):
    input_filename = os.path.join(root, file)
    print ("Process: " + input_filename)
    p = subprocess.Popen(
    'convert -density 200 '+input_filename+' -rotate 0.3 +noise Multiplicative -format pdf -quality 85 -compress JPEG -colorspace gray '+str(output_dir + file), shell=True)
    'convert -density 200 '+input_filename+' -rotate 0.3 +noise Multiplicative -format pdf -quality 85 -compress JPEG -colorspace gray '+str(output_dir + file),
    stdout=subprocess.PIPE, shell=True)
    err, out = p.communicate()
    if err:
    print(err)
    elif out:
    print(out)
    ```
  15. Benny Samir H revised this gist Nov 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ $ convert -density 200 INPUT.pdf -rotate 0.3 +noise Multiplicative -format pdf

    ## Batch-Scripte
    ### Python (2.7)
    ```
    ```python
    from __future__ import print_function
    import os
    import subprocess
  16. Benny Samir H revised this gist Nov 18, 2016. 1 changed file with 24 additions and 3 deletions.
    27 changes: 24 additions & 3 deletions simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,36 @@
    # Simulate a scanned PDF with ImageMagick
    1. Download and install ImageMagick + Convert Module
    1. Download and install [ImageMagick + Convert Module](https://www.imagemagick.org/script/binary-releases.php)
    2. Execute the following command in the console
    ```
    $ convert -density 200 INPUT.pdf -rotate 0.3 +noise Multiplicative -format pdf -quality 85 -compress JPEG -colorspace gray OUTPUT.pdf
    ```

    ### Description of the options
    ## Description of the options
    - **-density:** set the input DPI to 200
    - **-rotate:** set Page rotation to 0.3 degrees
    - **+noise:** add noise to the PDF (other Noise-Options: https://www.imagemagick.org/script/command-line-options.php#noise)
    - **-format:** set format to PDF for the correct sizing (A4)
    - **-quality:** set JPEG Quality to 85%
    - **-compress:** set compress to jpeg (other Compress-Options: https://www.imagemagick.org/script/command-line-options.php#compress)
    - **-colorspace:** set color to gray
    - **-colorspace:** set color to gray

    ## Batch-Scripte
    ### Python (2.7)
    ```
    from __future__ import print_function
    import os
    import subprocess
    input_dir = "Input/"
    output_dir = "Output/"
    if not os.path.exists(output_dir):
    os.makedirs(output_dir)
    for root, dirs, files in os.walk(input_dir):
    for file in files:
    if file.endswith(".pdf"):
    input_filename = os.path.join(root, file)
    print ("Process: " + input_filename)
    p = subprocess.Popen(
    'convert -density 200 '+input_filename+' -rotate 0.3 +noise Multiplicative -format pdf -quality 85 -compress JPEG -colorspace gray '+str(output_dir + file), shell=True)
    ```
  17. Benny Samir H revised this gist Nov 18, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    1. Download and install ImageMagick + Convert Module
    2. Execute the following command in the console
    ```
    $ convert -density 200 INPUT-FILENAME -rotate 0.3 +noise Multiplicative -format pdf -quality 85 -compress JPEG -colorspace gray OUTPUT-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
  18. Benny Samir H revised this gist Nov 18, 2016. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,10 @@ $ convert -density 200 INPUT-FILENAME -rotate 0.3 +noise Multiplicative -format
    ```

    ### Description of the options
    - -density set the input DPI to 200
    - -rotate set Page rotation to 0.3 degrees
    - +noise add noise to the PDF (other Noise-Options: https://www.imagemagick.org/script/command-line-options.php#noise)
    - -format set format to PDF for the correct sizing (A4)
    - -quality set JPEG Quality to 85%
    - -compress set compress to jpeg (other Compress-Options: https://www.imagemagick.org/script/command-line-options.php#compress)
    - -colorspace set color to gray
    - **-density:** set the input DPI to 200
    - **-rotate:** set Page rotation to 0.3 degrees
    - **+noise:** add noise to the PDF (other Noise-Options: https://www.imagemagick.org/script/command-line-options.php#noise)
    - **-format:** set format to PDF for the correct sizing (A4)
    - **-quality:** set JPEG Quality to 85%
    - **-compress:** set compress to jpeg (other Compress-Options: https://www.imagemagick.org/script/command-line-options.php#compress)
    - **-colorspace:** set color to gray
  19. Benny Samir H revised this gist Nov 18, 2016. No changes.
  20. Benny Samir H revised this gist Nov 18, 2016. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,11 @@
    Download and install ImageMagick + Convert Module

    # Simulate a scanned PDF with ImageMagick
    1. Download and install ImageMagick + Convert Module
    2. Execute the following command in the console
    ```
    $ convert -density 200 INPUT-FILENAME -rotate 0.3 +noise Multiplicative -format pdf -quality 85 -compress JPEG -colorspace gray OUTPUT-FILENAME
    ```

    ### Description of the options
    - -density set the input DPI to 200
    - -rotate set Page rotation to 0.3 degrees
    - +noise add noise to the PDF (other Noise-Options: https://www.imagemagick.org/script/command-line-options.php#noise)
  21. Benny Samir H revised this gist Nov 18, 2016. 2 changed files with 12 additions and 11 deletions.
    11 changes: 0 additions & 11 deletions simulate_scanned_PDF.cmd
    Original file line number Diff line number Diff line change
    @@ -1,11 +0,0 @@
    :: Download and install ImageMagick + Convert Module

    convert -density 300 INPUT-FILENAME -rotate 0.3 +noise Multiplicative -resample 200 -quality 85 -compress JPEG -colorspace gray OUTPUT-FILENAME

    :: -density set the input DPI to 300
    :: -rotate set Page rotation to 0.3 degrees
    :: +noise add noise to the PDF (other Noise-Options: https://www.imagemagick.org/script/command-line-options.php#noise)
    :: -resample set the output DPI to 200
    :: -quality set JPEG Quality to 85%
    :: -compress set compress to jpeg (other Compress-Options: https://www.imagemagick.org/script/command-line-options.php#compress)
    :: -colorspace set color to gray
    12 changes: 12 additions & 0 deletions simulate_scanned_PDF.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    Download and install ImageMagick + Convert Module

    ```
    $ convert -density 200 INPUT-FILENAME -rotate 0.3 +noise Multiplicative -format pdf -quality 85 -compress JPEG -colorspace gray OUTPUT-FILENAME
    ```
    - -density set the input DPI to 200
    - -rotate set Page rotation to 0.3 degrees
    - +noise add noise to the PDF (other Noise-Options: https://www.imagemagick.org/script/command-line-options.php#noise)
    - -format set format to PDF for the correct sizing (A4)
    - -quality set JPEG Quality to 85%
    - -compress set compress to jpeg (other Compress-Options: https://www.imagemagick.org/script/command-line-options.php#compress)
    - -colorspace set color to gray
  22. Benny Samir H revised this gist Nov 18, 2016. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions simulate_scanned_PDF.cmd
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,9 @@
    :: Download and install ImageMagick + Convert Module

    convert -density 300 INPUT-FILENAME -rotate 0.3 -compose divide -linear-stretch 5%x0% +noise Multiplicative -resample 200 -quality 85 -compress JPEG -colorspace gray OUTPUT-FILENAME
    convert -density 300 INPUT-FILENAME -rotate 0.3 +noise Multiplicative -resample 200 -quality 85 -compress JPEG -colorspace gray OUTPUT-FILENAME

    :: -density set the input DPI to 300
    :: -rotate set Page rotation to 0.3 degrees
    :: -compose set the type of image composition (other Compose-Options: https://www.imagemagick.org/script/command-line-options.php#compose)
    :: -linear-stretch (https://www.imagemagick.org/script/command-line-options.php#linear-stretch)
    :: +noise add noise to the PDF (other Noise-Options: https://www.imagemagick.org/script/command-line-options.php#noise)
    :: -resample set the output DPI to 200
    :: -quality set JPEG Quality to 85%
  23. Benny Samir H revised this gist Nov 18, 2016. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions simulate_scanned_PDF.cmd
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    :: Download and install ImageMagick + Convert Module

    convert -density 300 INPUT-FILENAME -rotate 0.3 -compose divide -linear-stretch 5%x0% +noise Multiplicative -resample 200 -quality 85 -compress JPEG OUTPUT-FILENAME
    convert -density 300 INPUT-FILENAME -rotate 0.3 -compose divide -linear-stretch 5%x0% +noise Multiplicative -resample 200 -quality 85 -compress JPEG -colorspace gray OUTPUT-FILENAME

    :: -density set the input DPI to 300
    :: -rotate set Page rotation to 0.3 degrees
    @@ -9,4 +9,5 @@ convert -density 300 INPUT-FILENAME -rotate 0.3 -compose divide -linear-stretch
    :: +noise add noise to the PDF (other Noise-Options: https://www.imagemagick.org/script/command-line-options.php#noise)
    :: -resample set the output DPI to 200
    :: -quality set JPEG Quality to 85%
    :: -compress set compress to jpeg (other Compress-Options: https://www.imagemagick.org/script/command-line-options.php#compress)
    :: -compress set compress to jpeg (other Compress-Options: https://www.imagemagick.org/script/command-line-options.php#compress)
    :: -colorspace set color to gray
  24. Benny Samir H revised this gist Nov 18, 2016. 1 changed file with 8 additions and 5 deletions.
    13 changes: 8 additions & 5 deletions simulate_scanned_PDF.cmd
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,12 @@
    convert -density 300 INPUT-FILENAME -resample 200 +noise Multiplicative -quality 85 -compose divide -linear-stretch 5%x0% -rotate 0.3 -compress jpeg OUTPUT-FILENAME
    :: Download and install ImageMagick + Convert Module

    convert -density 300 INPUT-FILENAME -rotate 0.3 -compose divide -linear-stretch 5%x0% +noise Multiplicative -resample 200 -quality 85 -compress JPEG OUTPUT-FILENAME

    :: -density set the input DPI to 300
    :: -resample set the output DPI to 200
    :: +noise add noise to the PDF (other Noise-Options: https://www.imagemagick.org/script/command-line-options.php#noise)
    :: -quality set JPEG Quality to 85%
    :: -rotate set Page rotation to 0.3 degrees
    :: -compose set the type of image composition (other Compose-Options: https://www.imagemagick.org/script/command-line-options.php#compose)
    :: -linear-stretch (https://www.imagemagick.org/script/command-line-options.php#linear-stretch)
    :: -rotate set Page rotation to 0.3 degrees
    :: +noise add noise to the PDF (other Noise-Options: https://www.imagemagick.org/script/command-line-options.php#noise)
    :: -resample set the output DPI to 200
    :: -quality set JPEG Quality to 85%
    :: -compress set compress to jpeg (other Compress-Options: https://www.imagemagick.org/script/command-line-options.php#compress)
  25. Benny Samir H revised this gist Nov 18, 2016. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion simulate_scanned_PDF.cmd
    Original file line number Diff line number Diff line change
    @@ -1 +1,9 @@
    convert -density 300 INPUT-FILENAME -resample 200 +noise Multiplicative -quality 85 -compose divide -linear-stretch 5%x0% -rotate 0.3 -compress jpeg OUTPUT-FILENAME
    convert -density 300 INPUT-FILENAME -resample 200 +noise Multiplicative -quality 85 -compose divide -linear-stretch 5%x0% -rotate 0.3 -compress jpeg OUTPUT-FILENAME
    :: -density set the input DPI to 300
    :: -resample set the output DPI to 200
    :: +noise add noise to the PDF (other Noise-Options: https://www.imagemagick.org/script/command-line-options.php#noise)
    :: -quality set JPEG Quality to 85%
    :: -compose set the type of image composition (other Compose-Options: https://www.imagemagick.org/script/command-line-options.php#compose)
    :: -linear-stretch (https://www.imagemagick.org/script/command-line-options.php#linear-stretch)
    :: -rotate set Page rotation to 0.3 degrees
    :: -compress set compress to jpeg (other Compress-Options: https://www.imagemagick.org/script/command-line-options.php#compress)
  26. Benny Samir H created this gist Nov 18, 2016.
    1 change: 1 addition & 0 deletions simulate_scanned_PDF.cmd
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    convert -density 300 INPUT-FILENAME -resample 200 +noise Multiplicative -quality 85 -compose divide -linear-stretch 5%x0% -rotate 0.3 -compress jpeg OUTPUT-FILENAME