Extract exif from photos

I had to classify some photos based on location and date, and I wrote a simple script combining exiftool and Openstreetmap.

#!/bin/bash

img=$1

date=$(exiftool -s -s -s -CreateDate $img)
lat_dms=$(exiftool -s -s -s -GPSLatitude $img)
lon_dms=$(exiftool -s -s -s -GPSLongitude $img)

#convert from degrees to decimals
dms2dec() {
	local dms=$1
	local dir=${dms: -1}
	dms=${dms%?}
	dms=${dms//deg/ }
	dms=${dms//\'/ }
	dms=${dms//\"/ }
	read -r deg min sec <<< "$dms"
	dec=$(echo "$deg + $min/60 + $sec/3600" | bc -l)
	if [[ $dir == "S" || $dir == "W" ]]; then
		dec=$(echo "-1 * $dec" | bc -l)
	fi
	printf "%.6f" "$dec"
}

lat=$(dms2dec "$lat_dms")
lon=$(dms2dec "$lon_dms")

#get the place from the coordinates
place=$(curl -s "https://nominatim.openstreetmap.org/reverse?lat=$lat&lon=$lon&format=json" -A "MyApp/1.0" | jq -r '.display_name')

#print in csv
echo "$img; $date; $place"

By running the script with for x in *HEIC; do ./extractExif.sh $x; done > index.csv one can get a list of lines like this one:

IMG_0000.HEIC; 2026:03:30 08:00:00; Some Street, Some Place, 12345, CountryName

The next step would be to move photos into folders or index them inside a database using index.csv as a driver.

Tags: bash photos
audit pixel