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

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

然后python擼了一段代碼 分析照片
分析下來(lái) emmm

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

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