add live brightness/exposure controls and a webcam.conf config file

exec-cycle.sh's live preview now shows brightness/exposure and lets
+/-, ./, adjust them per-frame via fswebcam --set, with r to reset;
webcam.conf centralizes the starting values, step size, capture
resolution/quality, and the v4l2 control names (which vary by camera)
so hardware-specific tuning doesn't require editing the scripts.
master
Amir Alexander Abdelbaki 2026-07-03 11:29:24 +02:00
parent 1ee9ed1036
commit ed7c0d0813
3 changed files with 77 additions and 12 deletions

View File

@ -2,22 +2,50 @@
trap 'stty sane; clear; exit 0' SIGINT SIGTERM
dir="$(dirname "$0")"
[ -f "$dir/webcam.conf" ] && source "$dir/webcam.conf"
BRIGHTNESS="${DEFAULT_BRIGHTNESS:-auto}"
EXPOSURE="${DEFAULT_EXPOSURE:-auto}"
STEP="${STEP:-5}"
# clamps $1+$2 to 0-100, treating "auto" as a 50 starting point
adjust() {
local cur=$1 delta=$2 val
[ "$cur" = "auto" ] && cur=50
val=$((cur + delta))
(( val < 0 )) && val=0
(( val > 100 )) && val=100
echo "$val"
}
while true; do
clear
export BRIGHTNESS EXPOSURE
./webcam2ascii.sh
echo
echo "[c] capture [Ctrl+C] quit"
echo "brightness: ${BRIGHTNESS} exposure: ${EXPOSURE}"
echo "[+/-] brightness [./,] exposure [r] reset to auto [c] capture [Ctrl+C] quit"
if read -n 1 -t 0.05 -s key && [[ "$key" == "c" || "$key" == "C" ]]; then
timestamp=$(date +%Y%m%d-%H%M%S)
cp web-cam-shot.jpg "photo-$timestamp.jpg"
cp ascii-img.txt "ascii-img-$timestamp.txt"
clear
cat "ascii-img-$timestamp.txt"
echo
echo "Saved photo-$timestamp.jpg and ascii-img-$timestamp.txt"
./txt2printr.sh "ascii-img-$timestamp.txt"
read -n 1 -s -p "Press any key to resume live preview, Ctrl+C to quit..."
if read -n 1 -t 0.05 -s key; then
case "$key" in
"+") BRIGHTNESS=$(adjust "$BRIGHTNESS" "$STEP") ;;
"-") BRIGHTNESS=$(adjust "$BRIGHTNESS" "-$STEP") ;;
".") EXPOSURE=$(adjust "$EXPOSURE" "$STEP") ;;
",") EXPOSURE=$(adjust "$EXPOSURE" "-$STEP") ;;
r|R) BRIGHTNESS="${DEFAULT_BRIGHTNESS:-auto}"; EXPOSURE="${DEFAULT_EXPOSURE:-auto}" ;;
c|C)
timestamp=$(date +%Y%m%d-%H%M%S)
cp web-cam-shot.jpg "photo-$timestamp.jpg"
cp ascii-img.txt "ascii-img-$timestamp.txt"
clear
cat "ascii-img-$timestamp.txt"
echo
echo "Saved photo-$timestamp.jpg and ascii-img-$timestamp.txt"
./txt2printr.sh "ascii-img-$timestamp.txt"
read -n 1 -s -p "Press any key to resume live preview, Ctrl+C to quit..."
;;
esac
fi
done

19
webcam.conf Normal file
View File

@ -0,0 +1,19 @@
# ASCII Photobooth webcam configuration
# Sourced by webcam2ascii.sh and exec-cycle.sh.
# Capture settings
RESOLUTION=640x480
JPEG_QUALITY=85
# Starting brightness/exposure (0-100, or "auto" for the camera default)
# exec-cycle.sh's live +/- and ./, controls adjust away from these at runtime
DEFAULT_BRIGHTNESS=auto
DEFAULT_EXPOSURE=auto
# Step size (percentage points) for each live-preview +/- adjustment
STEP=5
# v4l2 control names as reported by: fswebcam --list-controls
# These vary per camera/driver - edit to match your hardware
BRIGHTNESS_CONTROL="Brightness"
EXPOSURE_CONTROL="Exposure (Absolute)"

View File

@ -1,5 +1,23 @@
#/bin/bash
#
# Captures a frame and converts it to ascii art.
# Reads defaults from webcam.conf. BRIGHTNESS/EXPOSURE env vars (set by
# exec-cycle.sh's live preview controls) override the config's defaults.
fswebcam -q --no-banner -r 640x480 --jpeg 85 web-cam-shot.jpg &> /dev/null
dir="$(dirname "$0")"
[ -f "$dir/webcam.conf" ] && source "$dir/webcam.conf"
: "${RESOLUTION:=640x480}"
: "${JPEG_QUALITY:=85}"
: "${BRIGHTNESS:=${DEFAULT_BRIGHTNESS:-auto}}"
: "${EXPOSURE:=${DEFAULT_EXPOSURE:-auto}}"
: "${BRIGHTNESS_CONTROL:=Brightness}"
: "${EXPOSURE_CONTROL:=Exposure (Absolute)}"
set_opts=()
[ "$BRIGHTNESS" != "auto" ] && set_opts+=(--set "${BRIGHTNESS_CONTROL}=${BRIGHTNESS}%")
[ "$EXPOSURE" != "auto" ] && set_opts+=(--set "${EXPOSURE_CONTROL}=${EXPOSURE}%")
fswebcam -q --no-banner -r "$RESOLUTION" --jpeg "$JPEG_QUALITY" "${set_opts[@]}" web-cam-shot.jpg &> /dev/null
ascii-image-converter web-cam-shot.jpg | tee ascii-img.txt