Files
scan-color-samples/auto-scan.sh
2026-08-25 18:21:36 +03:30

246 lines
9.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# auto-scan.sh - end-to-end i1Pro 2 measurement workflow for ink samples.
#
# Steps:
# 1. Check required software (ArgyllCMS, python3); install if missing
# 2. Wait for the i1Pro 2 to be connected via USB
# 3. Run a spotread session (calibration + one reading per sample in samples.txt)
# 4. Convert the readings to a labelled CSV
# 5. Re-average all scan CSVs and rebuild averaged.csv (+ formula table)
#
# Usage:
# ./auto-scan.sh measure with D50 Lab (default)
# ./auto-scan.sh M1 measure with ISO 13655 M1 (also: M0, M2)
# ./auto-scan.sh --average-only skip scanning, just rebuild the averaged CSV
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
ARGYLL_VERSION="3.4.1"
OS="$(uname -s)"
case "$OS" in
Darwin) ARGYLL_PKG="osx10.6_x86_64" ;; # runs under Rosetta 2 on Apple Silicon
*) ARGYLL_PKG="linux_x86_64" ;;
esac
ARGYLL_URL="https://www.argyllcms.com/Argyll_V${ARGYLL_VERSION}_${ARGYLL_PKG}.tgz"
SAMPLES_FILE="samples.txt"
AVERAGED_CSV="averaged.csv"
FORMULA_CSV="formula.csv"
MODE="D50"
AVERAGE_ONLY=0
for arg in "$@"; do
case "$arg" in
D50|M0|M1|M2) MODE="$arg" ;;
--average-only) AVERAGE_ONLY=1 ;;
-h|--help)
sed -n '2,15p' "$0" | sed 's/^# \{0,1\}//'
exit 0
;;
*)
echo "Unknown argument: $arg (use D50, M0, M1, M2, or --average-only)" >&2
exit 1
;;
esac
done
info() { printf '\n\033[1;34m==> %s\033[0m\n' "$*"; }
warn() { printf '\033[1;33mWARNING: %s\033[0m\n' "$*" >&2; }
fail() { printf '\033[1;31mERROR: %s\033[0m\n' "$*" >&2; exit 1; }
# Lowercase helper: macOS ships bash 3.2, which lacks ${var,,}.
lower() { printf '%s' "$1" | tr '[:upper:]' '[:lower:]'; }
# ---------------------------------------------------------------- step 1
check_dependencies() {
info "Step 1/5: Checking required software"
if ! command -v python3 >/dev/null 2>&1; then
warn "python3 not found - attempting to install"
if [[ "$OS" == "Darwin" ]]; then
command -v brew >/dev/null 2>&1 \
|| fail "python3 not found. Install Homebrew (https://brew.sh), then: brew install python3"
brew install python3 || fail "Could not install python3 via brew"
elif command -v apt-get >/dev/null 2>&1; then
sudo apt-get update && sudo apt-get install -y python3 \
|| fail "Could not install python3"
else
fail "python3 not found and no supported package manager (apt-get/brew) available"
fi
fi
echo " python3: $(command -v python3)"
if ! command -v spotread >/dev/null 2>&1; then
# Look for an existing local ArgyllCMS install first
local argyll_bin
argyll_bin="$(ls -d "$HOME"/.local/bin/Argyll_V*/bin 2>/dev/null | sort | tail -1 || true)"
if [[ -n "$argyll_bin" ]]; then
export PATH="$argyll_bin:$PATH"
fi
fi
if ! command -v spotread >/dev/null 2>&1; then
warn "ArgyllCMS not found - downloading V${ARGYLL_VERSION}"
mkdir -p "$HOME/.local/bin"
local tgz="$HOME/.local/bin/Argyll_V${ARGYLL_VERSION}.tgz"
if command -v curl >/dev/null 2>&1; then
curl -fL -o "$tgz" "$ARGYLL_URL" || fail "Download failed: $ARGYLL_URL"
else
wget -O "$tgz" "$ARGYLL_URL" || fail "Download failed: $ARGYLL_URL"
fi
tar -zxf "$tgz" -C "$HOME/.local/bin"
rm -f "$tgz"
export PATH="$HOME/.local/bin/Argyll_V${ARGYLL_VERSION}/bin:$PATH"
command -v spotread >/dev/null 2>&1 || fail "ArgyllCMS install failed"
echo " Installed ArgyllCMS to ~/.local/bin/Argyll_V${ARGYLL_VERSION}"
echo " Add this to your shell profile (~/.bashrc or ~/.zshrc) to make it permanent:"
echo " export PATH=\"\$HOME/.local/bin/Argyll_V${ARGYLL_VERSION}/bin:\$PATH\""
fi
echo " spotread: $(command -v spotread)"
for helper in tsv-to-csv.py average-results.py prompt-next-sample.py; do
[[ -f "$helper" ]] || fail "Missing helper script: $helper"
done
if [[ "$OS" == "Linux" && ! -f /etc/udev/rules.d/55-Argyll.rules ]]; then
warn "Argyll udev rules not installed. If the instrument is not detected, run: sudo ./setup-udev.sh"
fi
}
# ---------------------------------------------------------------- step 2
wait_for_instrument() {
info "Step 2/5: Waiting for i1Pro 2"
local ports
while true; do
# spotread -? exits 1 (usage), so capture output rather than piping.
ports="$(spotread -? 2>&1 || true)"
if ! grep -q '\*\* No ports found \*\*' <<<"$ports"; then
break
fi
echo " No instrument detected. Connect the i1Pro 2 via USB."
read -r -p " Press Enter to check again (or Ctrl+C to abort)... "
done
grep -E "^[[:space:]]+[0-9]+ = " <<<"$ports" | sed 's/^/ Found:/' || true
}
# ---------------------------------------------------------------- step 3+4
scan_samples() {
[[ -f "$SAMPLES_FILE" ]] || fail "$SAMPLES_FILE not found"
# Strip a leading "*" (the Delta-E reference marker) from displayed names.
# A while-read loop is used instead of mapfile for bash 3.2 (macOS) support.
SAMPLES=()
while IFS= read -r line; do
SAMPLES+=("$line")
done < <(grep -v '^#' "$SAMPLES_FILE" | grep -v '^[[:space:]]*$' \
| sed -E 's/^[[:space:]]*\*[[:space:]]*//')
local count="${#SAMPLES[@]}"
[[ "$count" -gt 0 ]] || fail "No sample names found in $SAMPLES_FILE"
info "Step 3/5: Scanning $count samples (mode: $MODE)"
echo " Measure the samples in this exact order:"
local i=1
for s in "${SAMPLES[@]}"; do
printf ' %2d. %s\n' "$i" "$s"
i=$((i + 1))
done
echo ""
echo " spotread will start now:"
echo " - First calibrate: place the i1Pro 2 on its white tile and follow the prompt"
echo " - Then take one reading per sample (press the instrument button)"
echo " - The next sample code is printed in the terminal after each reading"
echo " - After all $count readings, press Q to finish"
echo ""
read -r -p " Press Enter to start... "
TIMESTAMP="$(date +%Y%m%d_%H%M%S)"
TSV="scan_${MODE}_${TIMESTAMP}.tsv"
local spotread_args=(-s "$TSV")
if [[ "$MODE" == "D50" ]]; then
spotread_args=(-i D50 "${spotread_args[@]}")
else
spotread_args=(-I "$MODE" "${spotread_args[@]}")
fi
python3 prompt-next-sample.py -s "$SAMPLES_FILE" -- spotread "${spotread_args[@]}"
[[ -f "$TSV" ]] || fail "No readings were saved ($TSV missing)"
local readings=$(($(wc -l < "$TSV") - 1))
if [[ "$readings" -ne "$count" ]]; then
warn "Expected $count readings but got $readings."
read -r -p " Keep this scan anyway? [y/N] " ans
if [[ "$(lower "$ans")" != "y" ]]; then
mkdir -p archive
mv "$TSV" "archive/${TSV%.tsv}_incomplete.tsv"
fail "Scan discarded (moved to archive/). Re-run ./auto-scan.sh"
fi
fi
info "Step 4/5: Saving labelled CSV"
./tsv-to-csv.py "$TSV" -s "$SAMPLES_FILE"
CSV="${TSV%.tsv}.csv"
echo " Scan saved: $CSV"
}
# ---------------------------------------------------------------- step 5
update_average() {
info "Step 5/5: Updating $AVERAGED_CSV"
local expected
expected="$(grep -v '^#' "$SAMPLES_FILE" | grep -cv '^[[:space:]]*$' || true)"
# Collect scan CSVs (timestamped files only) with the expected reading count
local -a scans=()
local f rows
for f in scan_"$MODE"_[0-9]*.csv; do
[[ -f "$f" ]] || continue
rows=$(($(wc -l < "$f") - 1))
if [[ "$rows" -eq "$expected" ]]; then
scans+=("$f")
else
warn "Skipping $f ($rows readings, expected $expected)"
fi
done
[[ "${#scans[@]}" -gt 0 ]] || fail "No complete scan CSVs found to average"
echo " Averaging ${#scans[@]} scan(s):"
printf ' %s\n' "${scans[@]}"
python3 average-results.py "${scans[@]}" -s "$SAMPLES_FILE" -o "$AVERAGED_CSV"
if [[ -f build-formula-table.py ]]; then
read -r -p " Rebuild $FORMULA_CSV as well? [Y/n] " ans
if [[ "$(lower "$ans")" != "n" ]]; then
python3 build-formula-table.py -i "$AVERAGED_CSV" -o "$FORMULA_CSV"
else
echo " Skipped $FORMULA_CSV (rebuild later with: python3 build-formula-table.py)"
fi
fi
echo ""
echo " Averaged L*a*b* values:"
python3 - "$AVERAGED_CSV" <<'PY'
import csv, sys
with open(sys.argv[1], newline="", encoding="utf-8") as f:
for row in csv.DictReader(f):
print(f" {row['sample_id']}: L*={float(row['L*_mean']):.2f}, "
f"a*={float(row['a*_mean']):.2f}, b*={float(row['b*_mean']):.2f} "
f"(n={row['n_scans']})")
PY
}
# ---------------------------------------------------------------- main
check_dependencies
if [[ "$AVERAGE_ONLY" -eq 1 ]]; then
update_average
else
wait_for_instrument
scan_samples
update_average
fi
info "All done."