正准備下班的python開發小哥哥
接到女朋友今晚要加班的電話
並給他發來一張背景模糊的自拍照
如下 ↓ ↓ ↓
敏感的python小哥 心裡一緊
難道會有原諒帽
關掉直播的小豬佩奇,手撸了一段python代碼
分析下來 emmm
拍攝地址居然在 XXX Hotel
小哥哥崩潰之余 大呼上當
小哥哥將發給自己的照片原圖下載下來
並使用python寫了一個腳本
讀取到了照片拍攝的詳細的信息
詳細到了具體的hotel
首先安裝python的exifread模塊,用於照片分析
pip install exifread 安裝exfriead模塊
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 json
其實我們平時拍攝的照片裡
隱藏了大量的信息
包括 拍攝時間、極其精確
具體的地址信息。
下面是通過exifread模塊
來讀取照片內的經緯度信息
#讀取照片的GPS經緯度信息
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():
#緯度
if re.match('GPS GPSLatitudeRef', tag):
GPS['GPSLatitudeRef'] = str(value)
#經度
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}
這裡需要使用調用百度API
將GPS經緯度信息轉換為具體的信息
這裡,你需要一個調用百度API的ak值
這個可以注冊一個百度開發者獲得
當然,你也可以使用博主的這個ak
調用之後,就可以將拍攝時間
拍攝詳細地址都解析出來。
def find_address_from_GPS(GPS):
secret_key = '自己的key'
if not GPS['GPS_information']:
return '該照片無GPS信息'
#經緯度信息
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)
#百度API轉換成具體的地址
content = response.text.replace("renderReverse&&renderReverse(", "")[:-1]
print(content)
baidu_map_address = json.loads(content)
#將返回的json信息解析整理出來
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:/女友自拍.jpg')
address = find_address_from_GPS(GPS=GPS_info)
print("拍攝時間:" + GPS_info.get("date_information"))
print('照片拍攝:' + str(address))
照片拍攝地址:('XX省
XXXXXXX縣',
'XX省', 'XXXX市', 'XXX縣',
'XXXX')
XXXXXX度假Hotel,
這明顯不是女友工作的地方
小哥哥搜索了一下
這是一家溫泉度假Hotel。
頓時就明白了
{"status":0,"result":{"location":{"lng":經度,"lat":緯度},
"formatted_address":"XX省XXXXXXXX縣",
"business":"",
"addressComponent":{"country":"China",
"country_code":0,
"country_code_iso":"CHN",
"country_code_iso2":"CN",
"province":"XX省",
"city":"XXXXX市",
"city_level":2,"district":XXX縣",
"town":"","town_code":"","adcode":"XXXXX",
"street_number":"",
"direction":"","distance":""},
"sematic_description":"XXXXX",
"cityCode":107}}
拍攝時間:2021:5:03 20:05:32
照片拍攝地址:('XX省XXXXXXX縣', 'XX省', 'XXXX市', 'XXX縣', 'XXXXX')
python定照片精確位置完整代碼腳本_Python手機定-Python文檔類資源-CSDN下載https://download.csdn.net/download/weixin_42350212/19776215