Record your study python Interesting little programs in the process , For entertainment only .
This article code implementation has the following several premises :
There's nothing to say about this , It's very easy to implement . Just call Exifread The module obtains the longitude and latitude information in the photo , Then call the reverse geocoding of Gaode map or Baidu map API, You can get the location in the picture .( This article takes the map of Gaud as an example )
Apply for goldmap account
Module installation
pip install exifread
import exifread
def get_long_and_lot(photo):
"""
:param photo: Incoming image path
:return:
"""
f = open(photo, 'rb') # Open the binary image
msg = exifread.process_file(f)
try:
exif_longitude = msg['GPS GPSLongitude'] # Picture longitude
exif_latitude = msg['GPS GPSLatitude'] # Picture latitude
exif_create_date = msg['EXIF DateTimeOriginal'] # Date the picture was created
print(exif_longitude, exif_latitude, exif_create_date)
# The value returned [113, 20, 1574707/62500] [23, 8, 4155487/200000] 2019:12:09 14:57:32
except:
print('Error!! The picture does not contain Gps Information ')
get_long_and_lot(' Picture path ')
exifread The information returned by the module is a dictionary
Take a look at the attached Gps The value returned by the image of the message , The main ones have been marked with arrows . The following code to achieve .
After getting the latitude and longitude of the picture , Then we will analyze the latitude and longitude .
Longitude, latitude, degree, minute, second conversion
once =60 branch , One point =60 second
such as :29.123456 Turn into Degrees, minutes and seconds Namely :29°7′24.44″
The integral part is degree , Multiply by a fraction 60 The whole number you get is minutes , Multiply the fractional part of a fraction 60 It's seconds
Don't talk too much about , It is suggested that you try it yourself .
import json
import requests
key = ' Yours key'
url = 'https://restapi.amap.com/v3/geocode/regeo?output=json&location={ longitude },{ latitude }&key={key}&radius=1000&extensions=base'
def get_location(exif_longitude, exif_latitude, exif_date):
"""
:param exif_longitude: Longitude in the picture
:param exif_latitude: Latitude in the picture
:param exif_date: Date the image was created
:return:
"""
long = list(str(i) for i in exif_longitude.values)
longitude = int(long[0]) + int(long[1]) / 60 + int(long[2].split('/')[0]) / int(long[2].split('/')[1]) / 3600
longitude = round(longitude, 6) # Longitude before decimal point 6 position
lat = list(str(i) for i in exif_latitude.values)
latitude = int(lat[0]) + int(lat[1]) / 60 + int(lat[2].split('/')[0]) / int(lat[2].split('/')[1]) / 3600
latitude = round(latitude, 6) # Latitude is before the decimal point 6 position
# Call up the Gaud map API
resp = requests.get(url.format(longitude, latitude, key))
# ' The returned value is json, Here we need to json Convert to dictionary '
location_msg = json.loads(resp.text)
location = location_msg['regeocode']['formatted_address']
print(' The picture was taken on :{}\n The longitude of the picture is :{}\n The latitude of the picture is :{}\n The address of the map is :{}'.format(exif_date, longitude, latitude, location))
print(' The answer is for reference only , Error in 1KM Inside ')
# The picture was taken on :2019:12:09 14:57:32
# The longitude of the picture is :113.340332
# The latitude of the picture is :23.139105
# The address of the map is : Tianhe District, Guangzhou City, Guangdong Province xxxxxx
# The answer is for reference only , Error in 1KM Inside
recommend :
In this way, you can avoid the threat that this article can bring to you .
In the information age 、 Everyone is naked . Tiger is more powerful than information , You must pay attention to protect your information security .
Okay , This sharing is here . If you have any questions, please leave a message below .