Created
May 28, 2024 16:08
-
-
Save CristianCantoro/009b23bdcf8529414914d78bc51c1674 to your computer and use it in GitHub Desktop.
Script to make a PDF look like it was scanned
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
#!/usr/bin/env bash | |
# shellcheck disable=SC2128 | |
SOURCED=false && [ "$0" = "$BASH_SOURCE" ] || SOURCED=true | |
if ! $SOURCED; then | |
set -euo pipefail | |
IFS=$'\n\t' | |
fi | |
#################### help | |
function short_usage() { | |
(>&2 echo \ | |
"Usage: | |
$(basename "$0") [options] <input_file>") | |
} | |
function usage() { | |
(>&2 short_usage ) | |
(>&2 echo \ | |
" | |
Make a PDF look like scanned. | |
Arguments: | |
input_file Username of the new user to add. | |
Options: | |
-d Enable debug output. | |
-o OUTPUT_FILE Ouput file name [default: output.pdf] | |
-h Show this help and exits. | |
Example: | |
$(basename "$0") input.pdf | |
") | |
} | |
debug=false | |
help_flag=false | |
output_file='output.pdf' | |
while getopts ":do:h" opt; do | |
case $opt in | |
d) | |
debug=true | |
;; | |
o) | |
output_file="$OPTARG" | |
;; | |
h) | |
help_flag=true | |
;; | |
\?) | |
(>&2 echo "Error. Invalid option: -$OPTARG") | |
exit 1 | |
;; | |
:) | |
(>&2 echo "Error.Option -$OPTARG requires an argument.") | |
exit 1 | |
;; | |
esac | |
done | |
if $help_flag; then | |
usage | |
exit 0 | |
fi | |
#################### end: help | |
#################### utils | |
if $debug; then | |
function echodebug() { | |
(>&2 echo -en "[$(date '+%F %H:%M:%S')][debug]: ") | |
(>&2 echo "$@" 1>&2) | |
} | |
else | |
function echodebug() { true; } | |
fi | |
#################### end: utils | |
# Shell Script: is mixing getopts with positional parameters possible? | |
# https://stackoverflow.com/q/11742996/2377454 | |
numopt="$#" | |
echodebug "numopt: $numopt" | |
echodebug "OPTIND: $OPTIND" | |
# parameter not specified | |
if (( numopt-OPTIND < 0 )) ; then | |
(>&2 echo "Error: parameter <input_file> is required.") | |
short_usage | |
exit 1 | |
fi | |
# unrecognized extra parameters not specified | |
if (( numopt-OPTIND > 0 )) ; then | |
(>&2 echo "Error: unrecognized extra parameters specified.") | |
short_usage | |
exit 1 | |
fi | |
input_file="${*:$OPTIND:1}" | |
echodebug "Arguments:" | |
echodebug " * input_file: $input_file" | |
echodebug | |
echodebug "Options:" | |
echodebug " * debug (-d): $debug" | |
echodebug " * output_file (-o): $output_file" | |
# Source: | |
# https://gist.github.com/andyrbell/25c8632e15d17c83a54602f6acde2724 | |
# | |
# 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_file" \ | |
-rotate 0.5 -attenuate 0.2 +noise Multiplicative -colorspace Gray \ | |
"$output_file" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment