scanpix/scanpix

120 lines
3.2 KiB
Bash
Executable File

#!/bin/bash
# scanpix
# Batch scan photos of the same size
# Copyright © 2016-2023 Keith Irwin ki9.gf4.pw
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Scans a photo, alerts the user when
# finished, and quits or scans another.
# Useful for bulk scanning photos while
# Doing other computer work.
#
# requires: SANE scanimage, zenity
# `sudo apt-get install scanimage zenity`
## VARIABLES
# Default scan area in mm
Y_SIZE='99' # width
X_SIZE='151' # height
## ARGUMENTS
if [ $# -gt 1 ]; then
echo "ERROR! Too many arguments."
exit 2
elif [ $# -eq 1 ]; then
DIR="${1}"
else
DIR="${PWD}"
fi
## RUNTIME
# Find and parse scanner name
echo -n "Finding scanner... "
SCANIMAGE="$(scanimage -L)"
SCANNER="$(<<<"${SCANIMAGE:8}" grep -oP ".*(?=')")"
if [ "${SCANNER}" == "" ]; then
echo "No scanner found!"
exit 3
fi
echo "found."
echo "Using scanner: ${SCANNER}"
# Ask user for a directory to scan into
DIR=$(zenity --entry --title="Scan Location" \
--text="Scan photos to (no trailing slash): " \
--entry-text="${DIR}")
# User didn't enter a valid directory
if [ ! -d "${DIR}" ]; then
zenity --error --title="Error: Scan location not found" \
--text="Couldn't find ${DIR}. Try again (no trailing slashes)"
exit 1
else echo "Saving photos to ${DIR}"; fi
# Ask for photo size
Y_SIZE=$(zenity --entry --title="Photo width" \
--text="How wide are the photos (in mm)?" \
--entry-text="${Y_SIZE}")
X_SIZE=$(zenity --entry --title="Photo height" \
--text="How high are the photos (in mm)?" \
--entry-text="${X_SIZE}")
echo "Scanning photos at ${X_SIZE} x ${Y_SIZE}"
# Wait for first photo to be placed
if ! zenity --question --title="Prepare scan" \
--text="Place the first photo on the scanner and click OK" \
--ok-label="OK" --cancel-label="Quit"
then exit 0; fi
# Repeat until user cancels
while true; do
# Repeat until scan succeeds or user quits
while true; do
# Execute scan
NOW="$(date +%s)"
FILENAME="${DIR}/${NOW}.jpg"
echo -n "Scanning ${NOW}.jpg... "
if ! scanimage \
--device-name="${SCANNER}" \
--mode=Color --resolution=1200 --format=tiff \
-x "${X_SIZE}" -y "${Y_SIZE}" \
| convert -rotate "180" tiff:- "${FILENAME}"
then echo "FAILED!"
if ! zenity --question --title="Scan Failed" \
--text="That photo didn't scan correctly! What do you want to do? " \
--ok-label="Try again" --cancel-label="Give up"
then exit 1; fi
# Scan succeeded
else echo "Done."
# Scan again?
if ! zenity --question --title="Scan Completed" \
--text="Photo scanned to ${NOW}.jpg\nNow what? " \
--ok-label="Scan another" --cancel-label="Quit"
then exit 0; fi
fi
done
done