實戰項目——獲取圖片中的GPS位置信息和拍攝時間


 

今天突然看到有人寫過獲取圖片中位置信息的程序。我覺得很有趣,也就自己實踐了一下,研究了一下

話不多說,先上代碼

 1 #!/usr/bin/env python3
 2 # -*- coding: utf-8 -*-
 3 
 4 'A program used to get GPS information in picture'
 5 
 6 __author__ = 'Albert Yang'
 7 
 8 import exifread
 9 import re
10 
11 def FindGPSTime(filePath):
12     GPS={}
13     Data=""
14     f=open(filePath,'rb')
15     tags=exifread.process_file(f)
16     #print("f:",f.read())
17     #print("tags:",tags)
18     #for key in tags:
19     #    print(key)
20 
21     for tag,value in tags.items():
22         if re.match('GPS GPSLatitudeRef',tag):
23             GPS['GPSLatitudeRef(緯度標識)']=str(value)
24         elif re.match('GPS GPSLongitudeRef',tag):
25             GPS['GPSLongitudeRef(經度標識)']=str(value)
26         elif re.match('GPS GPSAltitudeRef',tag):
27             GPS['GPSAltitudeRef(高度標識)']=str(value)
28         elif re.match('GPS GPSLatitude',tag):
29             try:
30                 match_result=re.match('\[(\w*), (\w*), (\w.*)/(\w.*)\]',str(value)).groups()   #匹配臨近的字符
31                 GPS['GPSLatitude(緯度)']=int(match_result[0]),int(match_result[1]),int(match_result[2])/int(match_result[3])
32             except:
33                 GPS['GPSLatitude(緯度)']=str(value)
34         elif re.match('GPS GPSLongitude',tag):
35             try:
36                 match_result=re.match('\[(\w*), (\w*), (\w.*)/(\w.*)\]',str(value)).groups()
37                 GPS['GPSLongitude(經度)']=[int(match_result[0]),int(match_result[1]),int(match_result[2])/int(match_result[3])]
38             except:
39                 GPS['GPSLongitude(經度)']=str(value)
40         elif re.match('GPS GPSAltitude',tag):
41             GPS['GPSAltitude(高度)']=str(value)
42         elif re.match('Image DateTime',tag):
43             Data=str(value)
44     return {'GPS 信息':GPS,'時間信息':Data}
45     #http: // www.gpsspg.com / maps.htm
46 
47 if __name__=='__main__':
48     print(FindGPSTime("3.jpg"))

 

幾個關鍵點:

1,exifread包就是用來專門獲取圖像的exif信息的。

2,exifread()函數返回字典,字典里的value值是IfdTag類型的。需要用str()轉化成str類型才好處理

3,re模塊\w是匹配字符,加上*大致是匹配多個的意思

這是我的運行結果

{'GPS 信息': {'GPSAltitudeRef(高度標識)': '0', 'GPSLatitude(緯度)': (**, **, *********), 'GPSAltitude(高度)': '0', 'GPSLongitude(經度)': [***, ***, *******], 'GPSLatitudeRef(緯度標識)': 'N', 'GPSLongitudeRef(經度標識)': 'E'}, '時間信息': '2018:08:31 16:03:06'}

里面的***是我人工打碼的。就是三個數字,分別是**°**’****”。就是經緯度的多少度,多少分,多少秒。

得到經緯度后

打開http://www.gpsspg.com/maps.htm這個網站。把經緯度信息輸進去就行啦。

注意輸入格式,先是緯度,再是經度。緯經度之間用逗號隔開。度,分,秒之間用空格隔開

 

具體的就是這樣。挺好玩的,不過根據我的實驗,QQ空間,朋友圈,微博里的圖片都提取不出。應該是因為都壓縮了。不過我讓我朋友發給我的照片都能提取出來。還行。

其他更好玩的你們再挖掘吧,覺得不錯記得贊一下


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM