Here’s what happened

Python development brother is getting ready to leave work

Got a call from my girlfriend that she had to work late tonight

And sent him a selfie with a blurry background

The following ↓ ↓ ↓

The sensitive little brother doubted whether there would be a cap for forgiveness

Python then handed over a photo of the code analysis

Analyze emMM

The location of the shooting was XXX hotel

The little brother broke down and shouted to be deceived

Analyzing photos in Python

The younger brother will send their own photos to download the original picture down

I wrote a script in Python

Got the exact address where the photo was taken

Down to specific street names and hotel names

Introduce the ExifRead module

Start by installing Python’s ExifRead module for photo analysis

PIP install exifRead Installs the Exfriead module

PS C:\WINDOWS\system32> PIP install exifRead Collecting exifRead Downloading exifread -2.3.2-py3-none-any.whl (38 kB) Installing collected Packages: ExifRead Successfully installed ExifRead -2.3.2 PS C:\WINDOWS\ System32 > PIP install JSONCopy the code

GPS latitude and longitude information

In fact, we usually take photos, hidden a lot of private information

Including the time of the shooting, extremely precise GPS information.

The following is the exifRead module to read the latitude and longitude information in the photo.

Def find_GPS_image(pic_path): GPS = {} date = '' with open(pic_path, 'rb') as f: Tags = exifread.process_file(f) for tag, value in tag.items (): # latitude if re.match('GPS GPSLatitudeRef', tag): GPS['GPSLatitudeRef'] = STR (value) # longitudere.match ('GPS GPSLongitudeRef', tag): GPS['GPSLongitudeRef'] = STR (value) # altitude elif re.match(' GPSAltitudeRef', tag): GPS['GPSAltitudeRef'] = str(value) elif re.match('GPS GPSLatitude', tag): try: match_result = re.match('\[(\w*),(\w*),(\w.*)/(\w.*)\]', str(value)).groups() GPS['GPSLatitude'] = int(match_result[0]), int(match_result[1]), int(match_result[2]) except: deg, min, sec = [x.replace(' ', '') for x in str(value)[1:-1].split(',')] GPS['GPSLatitude'] = latitude_and_longitude_convert_to_decimal_system(deg, min, sec) elif re.match('GPS GPSLongitude', tag): try: match_result = re.match('\[(\w*),(\w*),(\w.*)/(\w.*)\]', str(value)).groups() GPS['GPSLongitude'] = int(match_result[0]), int(match_result[1]), int(match_result[2]) except: deg, min, sec = [x.replace(' ', '') for x in str(value)[1:-1].split(',')] GPS['GPSLongitude'] = latitude_and_longitude_convert_to_decimal_system(deg, min, sec) elif re.match('GPS GPSAltitude', tag): GPS['GPSAltitude'] = str(value) elif re.match('.*Date.*', tag): date = str(value) return {'GPS_information': GPS, 'date_information': date}Copy the code

Baidu’S API transfers GPS addresses

Here you need to use the call Baidu API, GPS latitude and longitude information into specific address information.

Here, you need an AK value to call the Baidu API. This can be obtained by registering a Baidu developer. Of course, you can also use the AK of the blogger

After the call, you can take the time, take a detailed address are resolved.

def find_address_from_GPS(GPS): secret_key = 'zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf' if not GPS['GPS_information']: Lat, LNG = GPS['GPS_information']['GPSLatitude'], GPS['GPS_information']['GPSLongitude'] baidu_map_api = "http://api.map.baidu.com/geocoder/v2/?ak={0}&callback=renderReverse&location={1},{2}s&output=json&pois=0".format( secret_key, lat, LNG) response = request. get(baidu_map_api) # response.text.replace("renderReverse&&renderReverse(", "")[:-1] print(content) baidu_map_address = json.load (content baidu_map_address["result"]["formatted_address"] province = baidu_map_address["result"]["addressComponent"]["province"] city = baidu_map_address["result"]["addressComponent"]["city"] district = baidu_map_address["result"]["addressComponent"]["district"] location = baidu_map_address["result"]["sematic_description"] return formatted_address,province,city,district,location if __name__ == '__main__': GPS_info = find_GPS_image(pic_path='C:/ ') address = find_address_from_GPS(GPS=GPS_info) print(" "+ gps_info.get ("date_information")) print(' address :' + STR (address))Copy the code

The result wang got was this

Address :(128 meters southeast of building A, huquan hotel, maitreya county, honghe hani and yi autonomous prefecture, yunnan province)

Yunnan Maitreya Lake Spring hotel, which is obviously not where Lao Wang’s girlfriend works, Lao Wang searched, this is a hot spring resort hotel.

It was immediately clear

{" status ": 0," result ": {" location" : {" LNG ": 103.41424699999998," lat ": 24.410461020097278}, "AddressComponent ":{"country":" China ", "country_code":0, "country_code":0, "country_code":0, "country_code":0, "country_code":0, "country_code":0, "country_code":0, "country_code":0, "country_code":0, "Country_code_iso" : "CHN", "country_code_iso2" : "CN", "province", "yunnan province", "city" : "honghe hani and yi autonomous prefecture," "City_level" : 2, "the district", "mile county", "town" : ""," town_code ":" ", "adcode" : "532526", "street_number" : "", "Direction ":"","distance":"","distance":""," sematic_description":" Lake Spring Hotel - BLOCK A southeast 128m "," cityCode":107 Address :(128 meters southeast of Building A, Huquan Hotel, 'Maitreya County, honghe Hani and Yi Autonomous Prefecture, Yunnan Province ',' Maitreya County ', 'Honghe Hani and Yi Autonomous Prefecture, yunnan Province ')Copy the code

The complete code is as follows

Import exifRead import re import JSON import requests import OS # Latitude_and_longitude_convert_to_decimal_system (*arg): """ param arg: :return: Decimal fraction "" "return a float (arg [0]) + ((float (arg [1]) + (float (arg [2]. The split ('/') [0])/float (arg [2]. The split ('/') [1]) / 60)) / 60) def find_GPS_image(pic_path): GPS = {} date = "with open(pic_path, 'rb') as f: Tags = exifread.process_file(f) for tag, value in tag.items (): # latitude if re.match('GPS GPSLatitudeRef', tag): GPS['GPSLatitudeRef'] = STR (value) # longitudere.match ('GPS GPSLongitudeRef', tag): GPS['GPSLongitudeRef'] = STR (value) # altitude elif re.match(' GPSAltitudeRef', tag): GPS['GPSAltitudeRef'] = str(value) elif re.match('GPS GPSLatitude', tag): try: match_result = re.match('\[(\w*),(\w*),(\w.*)/(\w.*)\]', str(value)).groups() GPS['GPSLatitude'] = int(match_result[0]), int(match_result[1]), int(match_result[2]) except: deg, min, sec = [x.replace(' ', '') for x in str(value)[1:-1].split(',')] GPS['GPSLatitude'] = latitude_and_longitude_convert_to_decimal_system(deg, min, sec) elif re.match('GPS GPSLongitude', tag): try: match_result = re.match('\[(\w*),(\w*),(\w.*)/(\w.*)\]', str(value)).groups() GPS['GPSLongitude'] = int(match_result[0]), int(match_result[1]), int(match_result[2]) except: deg, min, sec = [x.replace(' ', '') for x in str(value)[1:-1].split(',')] GPS['GPSLongitude'] = latitude_and_longitude_convert_to_decimal_system(deg, min, sec) elif re.match('GPS GPSAltitude', tag): GPS['GPSAltitude'] = str(value) elif re.match('.*Date.*', tag): Date = STR (value) return {'GPS_information': GPS, 'date_information': date} Def find_address_from_GPS(GPS): """ converts latitude and longitude coordinates to a structured address using the Geocoding API. :param GPS: :return: """ secret_key = 'zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf' if not GPS['GPS_information']: Lat, LNG = GPS['GPS_information']['GPSLatitude'], GPS['GPS_information']['GPSLongitude'] baidu_map_api = "http://api.map.baidu.com/geocoder/v2/?ak={0}&callback=renderReverse&location={1},{2}s&output=json&pois=0".format( secret_key, lat, lng) response = requests.get(baidu_map_api) content = response.text.replace("renderReverse&&renderReverse(", "")[:-1] print(content) baidu_map_address = json.loads(content) formatted_address = baidu_map_address["result"]["formatted_address"] province = baidu_map_address["result"]["addressComponent"]["province"] city = baidu_map_address["result"]["addressComponent"]["city"] district = baidu_map_address["result"]["addressComponent"]["district"] location = baidu_map_address["result"]["sematic_description"] return formatted_address,province,city,district,location if __name__ == '__main__': GPS_info = find_GPS_image(pic_path='C:/Users/pacer/desktop/img/5.jpg') address = find_address_from_GPS(GPS=GPS_info) Print (" gps_info.get ("date_information")) print(' GPS_info :' + STR (address))Copy the code