import requests
import json
import base64
API_KEY = "Your API Key Here"
image_path = r"Image Here"
try:
with open(image_path, "rb") as image_file:
encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
except FileNotFoundError:
print("Error: Image file not found.")
exit()
find_place_url = f"
https://maps.googleapis.com/maps/api/place/findplacefromphoto/json?&key={API_KEY}"
payload = {
"photo": encoded_image,
"maxwidth": 1000
}
try:
response =
requests.post(find_place_url, data=payload, verify=False)
response.raise_for_status()
except requests.exceptions.HTTPError as error:
print(f"Error: {error}")
exit()
result = json.loads(response.text)
if result["status"] != "OK":
print("Error: Location not found.")
exit()
place_id = result["candidates"][0]["place_id"]
details_url = f"
https://maps.googleapis.com/maps/api/place/details/json?place_id={place_id}&key={API_KEY}"
try:
response = requests.get(details_url, verify=False)
response.raise_for_status()
except requests.exceptions.HTTPError as error:
print(f"Error: {error}")
exit()
result = json.loads(response.text)
if result["status"] != "OK":
print("Error: Location details not found.")
exit()
address = result["result"]["formatted_address"]
lat = result["result"]["geometry"]["location"]["lat"]
lng = result["result"]["geometry"]["location"]["lng"]
print(f"Address: {address}")
print(f"Latitude: {lat}")
print(f"Longitude: {lng}")