事情是這樣的
正準(zhǔn)備下班的python開發(fā)小哥哥
接到女朋友今晚要加班的電話
并給他發(fā)來一張背景模糊的自拍照
如下 ↓ ↓ ↓
敏感的小哥哥心生疑竇,難道會(huì)有原諒帽
然后python擼了一段代碼 分析照片
分析下來 emmm
拍攝地址居然在 xxx酒店
小哥哥崩潰之余 大呼上當(dāng)
python分析照片
小哥哥將發(fā)給自己的照片原圖下載下來
并使用python寫了一個(gè)腳本
讀取到了照片拍攝的詳細(xì)的地址
詳細(xì)到了具體的街道和酒店名稱
引入exifread模塊
首先安裝python的exifread模塊,用于照片分析
pip install exifread 安裝exfriead模塊
1
2
3
4
5
6
|
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信息。
下面是通過exifread模塊,來讀取照片內(nèi)的經(jīng)緯度信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
#讀取照片的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è)可以注冊一個(gè)百度開發(fā)者獲得,
當(dāng)然,你也可以使用博主的這個(gè)ak
調(diào)用之后,就可以將拍攝時(shí)間、拍攝詳細(xì)地址都解析出來。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
def find_address_from_gps(gps): secret_key = 'zblsuddl4cs2u0m4kezozzbguy9iwtvf' if not gps[ 'gps_information' ]: return '該照片無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=renderreverse&location={1},{2}s&output=json&pois=0" . format ( secret_key, lat, lng) response = requests.get(baidu_map_api) #百度api轉(zhuǎn)換成具體的地址 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 ( "拍攝時(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)容請搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/weixin_42350212/article/details/116568510