Skip to content

Instantly share code, notes, and snippets.

@marek-saji
Last active June 25, 2017 20:08
Show Gist options
  • Save marek-saji/5c5d5c1955da30ac592cab10cabf253d to your computer and use it in GitHub Desktop.
Save marek-saji/5c5d5c1955da30ac592cab10cabf253d to your computer and use it in GitHub Desktop.
#!/bin/bash
# GistID: 5c5d5c1955da30ac592cab10cabf253d
set -e
LOCATION_FILE="$HOME/.config/photo-locations.csv"
print_help ()
{
echo "Read information from file names and apply as EXIF."
echo
echo "Script expects for file names to have:"
echo " - date at the beginning,"
echo " - location name at the end, in brackets."
echo
echo "If locations are used, there must be a file ${LOCATION_FILE} in with three columns: location name, latitude and longitude."
echo
echo "Usage: $0 FILE..."
}
get_ref ()
{
if [ "${1//.*/}" -ge 0 ]
then
echo "$2"
else
echo "$3"
fi
}
if [ '-h' = "$1" ] || [ "--help" = "$1" ]
then
print_help
exit 0
fi
if [ 0 = $# ]
then
print_help
echo
echo "No input files given."
exit 64
fi
echo "Processing..."
for JPEG in "$@"
do
printf " - %s... " "${JPEG}"
OPTS=()
DATE="$(
echo "${JPEG}" | grep -oE '^[0-9]{4}(-[0-9]{2})[ T][0-9]{1,2}(:[0-9]{1,2}){2,3} ' || :
)"
if [ -n "${DATE}" ]
then
OPTS=( "${OPTS[@]}" -AllDates="${DATE}" )
fi
LOCATION_SUFFIX="$(
echo "${JPEG}" | grep -oE '\([^)]*\)\.[a-z]+$'
)"
LOCATION="$(
# shellcheck disable=SC2001
echo "${LOCATION_SUFFIX}" | sed -Ee 's/^\(|\).*//g'
)"
if [ -n "${LOCATION}" ]
then
LOCATION_LATLON="$( grep "^${LOCATION}," "${LOCATION_FILE}" | cut -d, -f2-3 )"
if [ -z "${LOCATION_LATLON}" ]
then
echo "location ${LOCATION} not found in ${LOCATION_FILE}"
else
LAT="$( echo "${LOCATION_LATLON}" | cut -d, -f1 )"
LON="$( echo "${LOCATION_LATLON}" | cut -d, -f2 )"
fi
else
LAT=
LON=
fi
if [ -n "${LAT}" ] && [ -n "${LON}" ]
then
LAT_REF="$( get_ref "${LAT}" N S )"
LON_REF="$( get_ref "${LON}" E W )"
OPTS=(
"${OPTS[@]}"
-GPSLatitude="${LAT}"
-GPSLatitudeRef="${LAT_REF}"
-GPSLongitude="${LON}"
-GPSLongitudeRef="${LON_REF}"
)
fi
if [ -z "${OPTS[*]}" ]
then
echo "no data found in, skipping"
else
exiftool -overwrite_original_in_place -quiet "${OPTS[@]}" "${JPEG}"
echo "done"
fi
done
echo "All done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment