Skip to content

Instantly share code, notes, and snippets.

@dmpop
Created November 14, 2021 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmpop/e8b8f3e6fd4612b225ca7a3572e4ece0 to your computer and use it in GitHub Desktop.
Save dmpop/e8b8f3e6fd4612b225ca7a3572e4ece0 to your computer and use it in GitHub Desktop.
Shell script for fixing non-geotagged photos
#!/usr/bin/env bash
# Author: Dmitri Popov, dmpop@linux.com
#######################################################################
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#######################################################################
if [ -z "$1" ]; then
echo "Please specify the file extension"
echo "For example: " $0 "JPG"
exit 1
fi
for file in *.$1; do
GPS=$(exiftool -gpslatitude $file)
if [ -z "$GPS" ]; then
echo "$file is not geotagged."
echo "Coordinates (example: 44.416111,12.201667):"
read coordinates
# Extract latitude and longitude from the $coordinates string using the cut tool
lat=$(echo "$coordinates" | cut -d',' -f1)
lon=$(echo "$coordinates" | cut -d',' -f2)
# Calculate the latitude and longitude references
# The latitude reference is N if the latitude value is positive
# The latitude reference is S if the latitude value is negative
# Use the bc tool to compare the value of the $lat variable and assign the correct latitude reference
if (($(echo "$lat > 0" | bc -l))); then
latref="N"
else
latref="S"
fi
# Calculate the correct longitude references for the given longitude value
# The longitude reference is E if the longitude value is positive
# The longitude reference is W if the longitude value is negative
if (($(echo "$lon > 0" | bc -l))); then
lonref="E"
else
lonref="W"
fi
# Write the obtained geographical coordinates into EXIF metadata of the file
echo $lat $lon
exiftool -overwrite_original -GPSLatitude=$lat -GPSLatitudeRef=$latref -GPSLongitude=$lon -GPSLongitudeRef=$lonref $file
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment