A very simple and interesting project recently discovered is to obtain the time of shooting and the GPS coordinates when shooting by analyzing the EXIF information of mobile phone photos. Then, the location corresponding to GPS can be translated by geographic inverse coding.
Interesting Uses
(1) Provide recall clues to photos that have forgotten information. By acquiring the time and place of shooting, it is easy to recall the scene of shooting at that time.
(2) Position the person you want to know. If you want a picture from someone else, you can know the location and time of the photographer.
Dependency Library
The program requires two dependency libraries: exifread, which is used to get EXIF information for pictures, and geopy, which is used to inversely encode the left side of GPS into geographic information.
Geographic information acquisition can also be done by registered developers of Gaode, Baidu and Tencent maps, but it is not convenient for geopy.
You can install two dependency Libraries
pip install exifread pip install geopy
Be careful:
Pictures must be original, original and original. (Without it, no pictures can be taken by Metu.)
Get picture EXIF information
Take this picture for example:
For a picture, in fact, the computer right-click, then click on properties, and then click on details, you will find that in fact the EXIF information of the picture has been able to view.
Now it's time to get all this information out of the exifread package.
import exifread img=exifread.process_file(open('picture.jpg','rb')) time=img['Image DateTime'] print(time) latitude=img['GPS GPSLatitude'] print(latitude) longitude=img['GPS GPSLongitude'] print(longitude)
img is actually a dictionary. Find out the corresponding time according to the key value of the dictionary. Longitude and latitude are all right.
But longitude and latitude are still in the form of GPS coordinates at this time.
The output is as follows:
2019:08:08 14:44:15 [34, 12, 9286743/200000] [108, 57, 56019287/1000000]
In this way, the required information is obtained.
Understanding of GPS coordinates:
Take the left side of the dimension as an example:
34 refers to degree, 12 is minute, 9286743/200000 is second, so latitude is 34 degrees, 12 minutes, 9286743/200000 seconds.
Formatting geographic coordinates
Geographic inverse decoding requires coordinates in decimal form, so the above coordinates should be converted according to
1 degree = 60 minutes; 1 minute = 60 seconds
Conversion of GPS coordinates
import exifread def format_lati_long(data):#list2float list_tmp=str(data).replace('[', '').replace(']', '').split(',') list=[ele.strip() for ele in list_tmp] data_sec = int(list[-1].split('/')[0]) /(int(list[-1].split('/')[1])*3600)# Second value data_minute = int(list[1])/60 data_degree = int(list[0]) result=data_degree + data_minute + data_sec return result img=exifread.process_file(open('picture.jpg','rb')) latitude=format_lati_long(str(img['GPS GPSLatitude'])) print(latitude) longitude=format_lati_long(str(img['GPS GPSLongitude'])) print(longitude)
The result of running the program is as follows:
34.21289825416667 108.96556091305555
In this way, the format conversion is completed.
Geographic decoding
The last step is to decode geographically and code directly.
from geopy.geocoders import Nominatim geolocator = Nominatim() position = geolocator.reverse('34.21289825416667,108.96556091305555')#Character format shows China print(position.address)
The output results are as follows:
Furong Garden West Gate, Furong West Road Fr ng West Rd, Qujiang, Yanta District, Yanta District, Xi'an City, Shaanxi Province, 710061, China
Okay, this is a picture taken during the summer vacation.
When the program runs, it may report warning. Don't worry, no problem, just wait for the result.
What matters is the following two questions:
First of all, the order of longitude and latitude: our usual habit is to first longitude and then dimension, for example: longitude (E) 120 degrees north latitude (N) 36 degrees. But when filling in, you should first dimension and then longitude, otherwise you will make a mistake.
Latitude and longitude coordinates must be filled in in character format, not float format, otherwise the location is abroad.
For example:
from geopy.geocoders import Nominatim geolocator = Nominatim() position = geolocator.reverse(34.21289825416667,108.96556091305555)#Character format shows China print(position.address)
The output of the above program is as follows:
RN 109, sidi khalifa, daïra Marhoum, Sidi Bel Abbès - سيدي بلعباس, ⴷⵣⴰⵢⵔ الجزائر
Or decode geographic information through the api of Gaode, Baidu and Tengxun maps
Take Goethe as an example:
Api_key is acquired by me by registering with Golden Developer Mode. How to get api_key has a tutorial on the internet, or you can use my api_key directly.
import requests,json api_key = 'f84cbb2dc078c087c6dc37b6ae74ab85' url_get_position = 'https://restapi.amap.com/v3/geocode/regeo?output=JSON&location={}&key={}&radius=1000&extensions=base' longitude=108.96556091305555 latitude=34.21289825416667 resp=requests.get(url_get_position.format(f'{longitude},{latitude}',api_key)) location_data = json.loads(resp.text) address = location_data.get('regeocode').get('formatted_address') print(address)
The output of the above program is as follows:
Jinyuehui (Qujiangdian) Mandy Square, Dayan Pagoda Street, Yanta District, Xi'an City, Shaanxi Province
The location is the same.
The complete code can be retrieved on github Code link