I can't seem to get the right longitude and latitude of images in Python
Question:
I am trying to extract the longitude and latitude of an image I took of myself. It went wrong horribly and said that I was in Japan, but I took the photo in Australia.
I tried this code:
import PIL.Image
import PIL.ExifTags
from geopy.geocoders import Nominatim
img = PIL.Image.open(r"C:UsersRGCRAPicturesCamera RollWIN_20240102_11_46_09_Pro.jpg")
exif = {
PIL.ExifTags.TAGS[k]: v
for k, v in img._getexif().items()
if k in PIL.ExifTags.TAGS
}
north = exif['GPSInfo'][2]
east = exif['GPSInfo'][4]
lat = ((((north[0] * 60) + north[1]) * 60) + north[2]) / 60 / 60
long = ((((east[0] * 60) + east[1]) * 60) + east[2]) / 60 / 60
lat, long = float(lat), float(long)
geoLoc = Nominatim(user_agent='GetLoc')
locname = geoLoc.reverse(f"{lat}, {long}")
print(locname.address)
I realized that I didn’t have a hyphen in front of the latitude. How could I fix it?
Answers:
I realized that since I am working in only the southern hemisphere, all I needed to do was:
lat = lat – (lat*2)
I don’t know how to do it worldwide, but this works for me.
I am trying to extract the longitude and latitude of an image I took of myself. It went wrong horribly and said that I was in Japan, but I took the photo in Australia.
I tried this code:
import PIL.Image
import PIL.ExifTags
from geopy.geocoders import Nominatim
img = PIL.Image.open(r"C:UsersRGCRAPicturesCamera RollWIN_20240102_11_46_09_Pro.jpg")
exif = {
PIL.ExifTags.TAGS[k]: v
for k, v in img._getexif().items()
if k in PIL.ExifTags.TAGS
}
north = exif['GPSInfo'][2]
east = exif['GPSInfo'][4]
lat = ((((north[0] * 60) + north[1]) * 60) + north[2]) / 60 / 60
long = ((((east[0] * 60) + east[1]) * 60) + east[2]) / 60 / 60
lat, long = float(lat), float(long)
geoLoc = Nominatim(user_agent='GetLoc')
locname = geoLoc.reverse(f"{lat}, {long}")
print(locname.address)
I realized that I didn’t have a hyphen in front of the latitude. How could I fix it?
I realized that since I am working in only the southern hemisphere, all I needed to do was:
lat = lat – (lat*2)
I don’t know how to do it worldwide, but this works for me.