Here’s the thing

A python developer getting ready to leave work

Got a call from my girlfriend about working late tonight

And sent him a selfie with a blurry background

↓ ↓ ↓

Sensitive little brother heart of doubt, can there be forgiveness cap

Python then extracts a piece of code to analyze the photo

Analyze the EMMM

The shooting address was actually in XXX Hotel

The little brother collapsed and shouted to be cheated

Python analysis of the photo brother downloaded the original photo sent to himself and wrote a script in Python to read the detailed address of the photo was taken, down to the specific street and hotel name. 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 timing of the photo, extremely precise and specific GPS information.

Here is the exifread module to read the longitude and latitude 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 tags.items(): # latitude if re.match('GPS GPSLatitudeRef', tag): tags = exifread.process_file(f) for tag, value in tags.items(): # latitude if re.match('GPS GPSLatitudeRef', tag): GPS['GPSLatitudeRef'] = STR (value) # longitude elif re.match('GPS GPSLongitudeRef', tag): GPS['GPSLongitudeRef'] = STR (value) # elif re.match('GPS 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 API will GPS to address

Here we need to use baidu API to convert GPS latitude and longitude information into specific address information.

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

After the call, the shooting time, shooting detailed address can be 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 = requests. Get (baidu_map_API) # Baidu_API convert content = response.text.replace("renderReverse&&renderReverse(", "")[:-1] print(content) zhen_map_address = json 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:/ selfie.jpg ') address = find_address_from_GPS(GPS=GPS_info) "+ gps_info.get ("date_information")) print(' address ')Copy the code

This is what Lao Wang got

Photo address :(‘ 128 meters southeast of block A, huquan hotel, ‘ ‘mille county, honghe hani and yi autonomous prefecture, yunnan province ‘)

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

Suddenly I understood

{" status ": 0," result ": {" location" : {" LNG ": 103.41424699999998," lat ": 24.410461020097278}, AddressComponent :{"country":" China ", "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":""," "sematic_description":" cityCode":107} Address of photos taken :(' 128 meters southeast of Building A, Huquan Hotel, ' 'Mille 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): """ Longitude and latitude convert to decimals, 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)) / Def find_GPS_image(pic_path): GPS = {} date = "with (pic_path, 'rb') as f: Tags = exifread.process_file(f) for tag, value in tags.items(): # latitude if re.match('GPS GPSLatitudeRef', tag): tags = exifread.process_file(f) for tag, value in tags.items(): # latitude if re.match('GPS GPSLatitudeRef', tag): GPS['GPSLatitudeRef'] = STR (value) # longitude elif re.match('GPS GPSLongitudeRef', tag): GPS['GPSLongitudeRef'] = STR (value) # elif re.match('GPS 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} # Use baidu Map API to convert GPS information to address. Def find_address_from_GPS(GPS): """ Convert latitude and longitude coordinates to structured addresses using the Geocoding API. :param GPS: :return: """ secret_key = 'zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf' if not GPS['GPS_information']: Return '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 ("date_information") print(' address ')Copy the code

Here is the end, we do this project in the process of encounter problems can click on the blue font for communication!

Recently, many friends have sent messages to ask about learning Python. For easy communication, click on blue to join yourselfDiscussion solution resource base