scanpix/scanpix

152 lines
3.4 KiB
Bash
Executable File

#!/bin/bash
# scanpix
# Batch scan photos of the same size
# Copyright © 2016-2021 Keith Irwin www.ki9.us
#
# 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="152" # height
## ARGUMENTS
if [ $# -gt 1 ]; then
echo "ERROR! Too many arguments."
exit 2
elif [ $# -eq 1 ]; then
FOLDER="$1"
else
FOLDER="$PWD"
fi
## RUNTIME
# Find and parse scanner name
echo -n "Finding scanner... "
SCANIMAGE=$(scanimage -L)
SCANNER=$(echo "${SCANIMAGE:8}" | grep -oP ".*(?=')")
if [ "$SCANNER" == "" ]; then
echo "no scanner found!"
exit 3
fi
echo "found."
echo "Using scanner: $SCANNER"
# Ask user for a folder to scan into
FOLDER=$(zenity --entry \
--title="Scan Location" \
--text="Scan photos to (no trailing slash): " \
--entry-text="$FOLDER")
# User didn't enter a valid folder
if [ ! -d "$FOLDER" ]; then
zenity --error \
--title="Error: Scan location not found" \
--text="Couldn't find $FOLDER. Try again (no trailing slashes)"
exit 1
else
echo "Saving photos to $FOLDER"
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
zenity --question \
--title="Prepare scan" \
--text="Place the first photo on the scanner and click OK" \
--ok-label="OK" \
--cancel-label="Quit"
# Cancel
if [ ! $? = 0 ]; 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="$FOLDER/$NOW.jpg"
echo -n "Scanning $NOW.jpg... "
scanimage \
--device-name="$SCANNER" \
--mode=Color \
--resolution=1200 \
--format=tiff \
-x "$X_SIZE" -y "$Y_SIZE" \
| convert \
-rotate "180" \
tiff:- "$FILENAME"
# Scan failed
if [ $? = 1 ]; then
echo "FAILED!"
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"
if [ ! $? = 0 ]; then
# Give up
exit 1
fi
# Scan succeeded
else
echo "Done."
# Scan again?
zenity --question \
--title="Scan Completed" \
--text="Photo scanned to $NOW.jpg\nNow what? " \
--ok-label="Scan another" \
--cancel-label="Quit"
# No
if [ ! $? = 0 ]; then
exit 0
fi
fi
done
done