Created
August 16, 2025 14:10
-
-
Save rmpel/d56fcb4fb818a7da3250eb0e1ba30dac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# usage; cron entry to run this script every minute. | |
# the script will take 3 snaps at most, at run, run+20, run+40. Next minute is a new iteration. | |
# script construction allows changes between snaps, as the script runs itself 3x. | |
cd "$(dirname "$0")" | |
# Set default values for environment variables | |
: "${HTTP_URL:=https://webcam.connect.prusa3d.com/c/snapshot}" | |
: "${DELAY_SECONDS:=20}" | |
: "${LONG_DELAY_SECONDS:=60}" | |
FINGERPRINT="You need a unique string here to identify this camera" | |
TOKEN="You also need a camera token from the Connect interface" | |
SNAPSHOTURL="http://127.0.0.1/webcam/?action=snapshot" | |
LOG="./snap_to_prusa/snaps-"$(date '+%Y-%m-%d')".log" | |
do_snap() { | |
# nigtmode camera experimental. | |
nightmode=YES | |
[ "day" = "$1" ] && nightmode=NO | |
# grab from streamer | |
curl -s "$SNAPSHOTURL" -o ./snap_to_prusa/output.jpg 2>/dev/null | |
# If no error, upload it. | |
if [ $? -eq 0 ]; then | |
datestr=$(date +"%a %b %d %H:%M:%S %Z %Y") | |
if [ "NO" = "$nightmode" ]; then | |
convert ./snap_to_prusa/output.jpg \ | |
-quality 85 \ | |
-filter lanczos \ | |
-modulate 100,150,100 \ | |
-channel R -level 20%,80% \ | |
-channel B -level 30%,100% \ | |
-channel G -level 0%,90% \ | |
+channel \ | |
-pointsize 36 \ | |
-font 'Helvetica-Neue-Light' \ | |
-fill white \ | |
-undercolor '#00000080' \ | |
-gravity southwest \ | |
-annotate +10+10 "${datestr}" ./snap_to_prusa/annotated.jpg 2>> "$LOG" | |
fi | |
if [ "YES" = "$nightmode" ]; then | |
convert ./snap_to_prusa/output.jpg \ | |
-quality 85 \ | |
-filter lanczos \ | |
-pointsize 36 \ | |
-font 'Helvetica-Neue-Light' \ | |
-fill white \ | |
-undercolor '#00000080' \ | |
-gravity southwest \ | |
-annotate +10+10 "${datestr}" ./snap_to_prusa/annotated.jpg 2>> "$LOG" | |
fi | |
# POST the image to the HTTP URL using curl | |
curl -X PUT "$HTTP_URL" \ | |
-H "accept: */*" \ | |
-H "content-type: image/jpg" \ | |
-H "fingerprint: $FINGERPRINT" \ | |
-H "token: $TOKEN" \ | |
--data-binary "@${PWD}/snap_to_prusa/annotated.jpg" \ | |
-s \ | |
--compressed \ | |
> /dev/null 2>> "$LOG" | |
else | |
echo "$datestr: Snapshot returned an error. Skipping this cycle." >> "$LOG" | |
fi | |
} | |
if [ "do_snap" = "$1" ]; then | |
do_snap "$@" | |
else | |
for counter in {1..3}; do | |
./"$(basename "$0")" do_snap "$@" | |
if [ $counter -lt 3 ]; then | |
sleep "$DELAY_SECONDS" | |
fi | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment