Skip to content

Instantly share code, notes, and snippets.

@nanawel
Last active June 7, 2022 12:05
Show Gist options
  • Save nanawel/baf17a1ff1354b79884e3f649b9cd09f to your computer and use it in GitHub Desktop.
Save nanawel/baf17a1ff1354b79884e3f649b9cd09f to your computer and use it in GitHub Desktop.
Set or update OffsetTime and OffsetTimeOriginal EXIF tags on pictures using current timezone and pictures DateTimeOriginal
#!/bin/bash
DIR="${DIR:-$1}"
OVERWRITE_EXISTING_TZ="${OVERWRITE_EXISTING_TZ:-0}"
ECHO_ONLY="${ECHO_ONLY:-0}"
[ ! -z "$DIR" ] || { echo -e >&2 "Usage:\n\t$0 <directory>"; exit 1; }
trap 'echo -e >&2 "\nCanceled."; exit 255;' INT
cmdPrefix="$([ $ECHO_ONLY = "1" ] && echo "echo" || echo "")"
IFS=$'\n'; for f in $(find "$DIR" -type f -iname "*.jpg"); do
file_date="$(exiftool -s -s -s -d '%Y-%m-%d %H:%M:%S' -DateTimeOriginal "$f")"
file_tz="$(exiftool -s -s -s -OffsetTime "$f")"
date_tz=$(date -d "$file_date" +%z | sed 's/\(.[0-9]\{2\}\)\([0-9]\{2\}\)/\1:\2/')
echo -n >&2 "$f => DATE=$file_date | TZ=$file_tz => "
if [ -z "$file_tz" ] || ([ "$file_tz" != "$date_tz" ] && [ "$OVERWRITE_EXISTING_TZ" = "1" ]); then
echo >&2 "Fix needed"
$cmdPrefix exiftool -OffsetTime="$date_tz" -OffsetTimeOriginal="$date_tz" $(printf %q "$f")
else
echo >&2 "OK"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment