From c84a1c8cff8af49844e08f86496c7467552ed414 Mon Sep 17 00:00:00 2001 From: Jordan Atwood Date: Tue, 3 Jan 2023 11:04:42 -0800 Subject: [PATCH] Add manuscan script This additionally leads to `$HOME/.bin` being created via dotfiles setup rather than manually by `.bash_profile`, so that functionality is removed. --- src/.bash_profile | 3 +-- src/.bin/manuscan.sh | 57 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/.bin/manuscan.sh diff --git a/src/.bash_profile b/src/.bash_profile index 0ea3486..0e1a34c 100755 --- a/src/.bash_profile +++ b/src/.bash_profile @@ -7,8 +7,7 @@ if [ -f ~/.profile ]; then fi ### environment vars -# Ensure $HOME/.bin exists, add it to PATH -mkdir -vp "$HOME/.bin" +# add $HOME/.bin to PATH (this is created via dotfiles setup) export PATH="${PATH}:$HOME/.bin" # some programs (pip, notably) install into ~/.local/bin, add that to PATH too diff --git a/src/.bin/manuscan.sh b/src/.bin/manuscan.sh new file mode 100644 index 0000000..b64fc04 --- /dev/null +++ b/src/.bin/manuscan.sh @@ -0,0 +1,57 @@ +#!/usr/bin/env bash + +### +### Simple scanning script using `scanimage` and `gs` to scan a series of images +### from a flatbed scanner (continuing to new pages or breaking to a new +### document based on user input as needed) in lieu of access to a feeding +### scanner. +### + +set -e + +SCAN_OUTPUT_DIR=~/Pictures/Scans +TMP_DIR="$(mktemp -d --suffix='scans')" + +while + scan_output_filename=scan_"$(date +%Y-%m-%d-%H-%M-%S)" + + page_number=1 + while + echo "Scanning page $page_number" + scanimage --format tiff --resolution 150 --output "$TMP_DIR/scan$(printf '%02d' "$page_number").tiff" + echo + + read -r -n 1 -p "Press enter to scan page $(( page_number + 1 )), 'n' to start scanning a new document, or any other character to stop scanning: " prompt + [[ -z "$prompt" ]] + do + (( page_number++ )) + done + echo + + # convert image sequence to pdf + convert "$TMP_DIR"/*.tiff "${SCAN_OUTPUT_DIR}/${scan_output_filename}_raw.pdf" + + # compress pdf + # see: https://itsfoss.com/compress-pdf-linux/ + gs \ + -sDEVICE=pdfwrite \ + -dCompatibilityLevel=1.4 \ + -dPDFSETTINGS=/prepress \ + -dNOPAUSE \ + -dQUIET \ + -dBATCH \ + -sOutputFile="${SCAN_OUTPUT_DIR}/${scan_output_filename}.pdf" \ + "${SCAN_OUTPUT_DIR}/${scan_output_filename}_raw.pdf" + + # remove temp files + rm -rf "${TMP_DIR:?}"/* "${SCAN_OUTPUT_DIR}/${scan_output_filename}_raw.pdf" + + echo "Scanned $(( page_number )) pages to ${SCAN_OUTPUT_DIR}/${scan_output_filename}.pdf" + echo + + [[ "$prompt" == 'n' || "$prompt" == 'N' ]] +do + true +done + +rm -rf "$TMP_DIR"