Created
June 7, 2022 00:24
-
-
Save lordjabez/6275938a546aa554c61165a08aeb4117 to your computer and use it in GitHub Desktop.
Python script to make a set of images look like they were scanned
This file contains hidden or 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
#!/usr/bin/env python3 | |
# Pre-requisite: brew install imagemagick | |
# Usage: ./generate-scans.py NUM_SCANS_PER_FILE | |
import multiprocessing | |
import random | |
import subprocess | |
import os | |
import sys | |
scans_per_doc = int(sys.argv[1]) | |
def make_scan(input_filename, output_filename): | |
print(input_filename, output_filename) | |
gamma = random.choice(range(11)) / 10.0 + 0.5 | |
stretch_x = random.choice(range(11)) | |
stretch_y = random.choice(range(11)) | |
rotate = random.choice(range(21)) / 5.0 - 2.0 | |
input_params = f'-density 300 {input_filename}[0]' | |
variable_params = f'-gamma {gamma} -linear-stretch {stretch_x}%x{stretch_y}% -rotate {rotate}' | |
output_params = f'-colorspace gray -quality 95 {output_filename}' | |
command = f'convert {input_params} {variable_params} {output_params}' | |
subprocess.run(command, shell=True) | |
def make_scans(input_filename): | |
for index in range(scans_per_doc): | |
output_filename = input_filename.replace('.pdf', f'-{index:04}.pdf') | |
make_scan(input_filename, output_filename) | |
filename_list = [d for d in os.listdir('.') if d.endswith('.pdf')] | |
if __name__ == '__main__': | |
pool = multiprocessing.Pool() | |
pool.map(make_scans, filename_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment