forked from Wileysec/Photo_GPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
photo_gps.py
104 lines (91 loc) · 4.38 KB
/
photo_gps.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import exifread
import re
import json
import requests
import sys
def latitude_and_longitude_convert_to_decimal_system(*arg):
return float(arg[0]) + ((float(arg[1]) + (float(arg[2].split('/')[0]) / float(arg[2].split('/')[-1]) / 60)) / 60)
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()
print(match_result)
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}
def ImageInfo():
try:
information = {}
with open(pic_path, 'rb') as f:
tags = exifread.process_file(f)
information['Image Make'] = tags['Image Make']
information['Image Model'] = tags['Image Model']
information['Image DateTime'] = tags['Image DateTime']
return information
except:
pass
def find_address_from_GPS(GPS):
if not GPS['GPS_information']:
return '该照片无GPS信息'
else:
lat, lng = GPS['GPS_information']['GPSLatitude'], GPS['GPS_information']['GPSLongitude']
access = "http://map.yanue.net/gpsapi.php?lat={0}&lng={1}".format(lat, lng)
accres = requests.get(access)
accres = accres.text
accres = json.loads(accres)
if accres['error'] == 0:
api = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=你的百度地图AK密钥&output=json&coordtype=wgs84ll&location={0},{1}".format(accres['baidu']['lat'],accres['baidu']['lng'])
response = requests.get(api)
content = response.text
address = json.loads(content)
else:
api = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=你的百度地图AK密钥&output=json&coordtype=wgs84ll&location={0},{1}".format(lat, lng)
response = requests.get(api)
content = response.text
address = json.loads(content)
if address['status'] == 0:
add = address['result']['formatted_address']
business = address['result']['business']
country = address['result']['addressComponent']['country']
text = country + add + business + " 经度(lng):" + str(lng) + " 纬度(lat):" + str(lat)
return text
else:
return "地址查询失败 经度(lng):" + str(lng) + " 纬度(lat):" + str(lat)
def main(pic_path):
pic_path = sys.argv[1]
GPS_info = find_GPS_image(pic_path)
address = find_address_from_GPS(GPS=GPS_info)
information = ImageInfo()
try:
info = "手机厂商:" + str(information['Image Make']) + " 手机型号:" + str(information['Image Model']) + " 拍摄时间:" + str(
information['Image DateTime']) + " " + address
print(info)
except:
pass
if __name__ == "__main__":
pic_path = sys.argv[1]
main(pic_path)