事情是這樣的
正準備下班的python開發(fā)小哥哥
接到女朋友今晚要加班的電話
并給他發(fā)來一張背景模糊的自拍照
如下 ↓ ↓ ↓

敏感的小哥哥心生疑竇,難道會有原諒帽

然后python擼了一段代碼 分析照片
分析下來 emmm

拍攝地址居然在 XXX酒店
小哥哥崩潰之余 大呼上當

python分析照片
小哥哥將發(fā)給自己的照片原圖下載下來
并使用python寫了一個腳本
讀取到了照片拍攝的詳細的地址
詳細到了具體的街道和酒店名稱
引入exifread模塊
首先安裝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
GPS經緯度信息
其實我們平時拍攝的照片里,隱藏了大量的私密信息
包括 拍攝時間、極其精確 具體的GPS信息。
下面是通過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,將GPS經緯度信息轉換為具體的地址信息。
這里,你需要一個調用百度API的ak值,這個可以注冊一個百度開發(fā)者獲得,
當然,你也可以使用博主的這個ak
調用之后,就可以將拍攝時間、拍攝詳細地址都解析出來。
def find_address_from_GPS(GPS):
secret_key = 'zbLsuDDL4CS2U0M4KezOZZbGUY9iWtVf'
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=renderReverselocation={1},{2}soutput=jsonpois=0".format(
secret_key, lat, lng)
response = requests.get(baidu_map_api)
#百度API轉換成具體的地址
content = response.text.replace("renderReverserenderReverse(", "")[:-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))
Python小哥得到的結果是這樣的
照片拍攝地址:('云南省XXXXXXX縣', '云南省', 'XXXX市', 'XXX縣', 'XXXX酒店')
云南彌勒XXXX酒店,這明顯不是老王女友工作的地方
小哥哥搜索了一下,這是一家溫泉度假酒店。
頓時就明白了
完整代碼:點此下載
到此這篇關于女友半夜加班發(fā)自拍 python男友用30行代碼發(fā)現(xiàn)驚天秘密的文章就介紹到這了,更多相關python讀取GPS內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 詳解python中文編碼問題
- 用基于python的appium爬取b站直播消費記錄
- 前女友發(fā)來加密的"520快樂.pdf",我用python破解開之后,卻發(fā)現(xiàn)
- 在前女友婚禮上用python把婚禮現(xiàn)場的WIFI名稱改成了