实战项目——获取图片中的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空间,朋友圈,微博里的图片都提取不出。应该是因为都压缩了。不过我让我朋友发给我的照片都能提取出来。还行。

其他更好玩的你们再挖掘吧,觉得不错记得赞一下

猜你喜欢

转载自www.cnblogs.com/albert-yzp/p/9566940.html