Skip to content

Instantly share code, notes, and snippets.

@say4n
Created August 24, 2025 17:48
Show Gist options
  • Select an option

  • Save say4n/87cd08c243282c5f4b738d52d7cb8730 to your computer and use it in GitHub Desktop.

Select an option

Save say4n/87cd08c243282c5f4b738d52d7cb8730 to your computer and use it in GitHub Desktop.
Time and location stamp images from EXIF metadata.
#!/bin/bash
# --- Function to process a single image file ---
process_file() {
# The file path is passed as the first argument to the function
local FILE="$1"
# Check if the file exists before proceeding
if [ ! -f "$FILE" ]; then
echo "Error: File not found: $FILE" >&2
return 1 # Exit the function with an error code
fi
echo "Processing: $FILE"
# Extract metadata into variables
local DATETIME
DATETIME=$(exiftool -s -s -s -d "%d-%b-%Y %H:%M:%S" -DateTimeOriginal "$FILE")
local LOCATION
LOCATION=$(exiftool -s -s -s -c "%.6f" -GPSPosition "$FILE")
# Check if location data was found; if not, skip this file
if [ -z "$LOCATION" ]; then
echo "Skipping '$FILE': No GPS data found." >&2
return 1
fi
# Get the original filename without the path and extension
local FILENAME_NO_EXT
FILENAME_NO_EXT=$(basename -- "$FILE")
FILENAME_NO_EXT="${FILENAME_NO_EXT%.*}"
# Generate the output filename
local OUT_FILE="time_loc_${FILENAME_NO_EXT}.jpg"
# Annotate the image using the extracted metadata
magick "$FILE" \
-gravity SouthEast \
-pointsize 32 \
-font "Helvetica-Bold" \
-fill white \
-annotate +30+60 "$LOCATION" \
-annotate +30+95 "$DATETIME" \
"$OUT_FILE"
echo "Annotated image saved as '$OUT_FILE'"
}
# --- Main script logic to read input ---
# Check if input is being piped to the script
if [ -p /dev/stdin ]; then
# Read each line from the pipe and process it as a file
while IFS= read -r piped_file; do
process_file "$piped_file"
done
# Check if filenames were provided as arguments
elif [ "$#" -gt 0 ]; then
# Loop through all arguments and process each one as a file
for arg_file in "$@"; do
process_file "$arg_file"
done
# If no input was provided either way, show a usage message
else
echo "📸 Usage:"
echo " As an argument: $0 image1.HEIC image2.JPG"
echo " Via a pipe: find . -name '*.HEIC' | $0"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment