Skip to content

Instantly share code, notes, and snippets.

@elfsternberg
Created November 15, 2025 22:16
Show Gist options
  • Select an option

  • Save elfsternberg/aab94aa0155234409a94d1f10b0790de to your computer and use it in GitHub Desktop.

Select an option

Save elfsternberg/aab94aa0155234409a94d1f10b0790de to your computer and use it in GitHub Desktop.
webcam-compress: Monitor a folder and as the webcam dumps JPGs into it, convert them to WEBP
#!/usr/bin/env bash
# A very simple script to watch a folder for new images. When new images
# are written to the folder, automatically convert them to webp and, if the
# conversion is successful, delete the JPG.
# Require ImageMagick and inotify-tools
set -o errexit
set -o nounset
shopt -s nullglob
inotifywait -m -e close_write --include '\.jpg$' . |
while read -r _path _action file; do
if [[ "$file" == *".jpg" ]]; then
sleep 1
for jpg in *.jpg ; do
convert "$jpg" $(echo "$jpg" | sed 's/\.jpg$/.webp/') && rm "$jpg"
echo "Converted $jpg to webp format."
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment