import re
import exifread
#获取图片的GPS信息
def FindGPSimage(filepath):
with open(filepath, 'rb') as f:
tags = exifread.process_file(f)
# print(tags)
GPS = {}
date = ''
for tag, value in tags.items():
# print(tag, value)
if re.match('GPS GPSLatitudeRef', tag):
GPS['GPSLatitudeRef(纬度标识)'] = str(value)
if re.match('GPS GPSLongitudeRef', tag):
GPS['GPSLongitudeRef(经度标识)'] = str(value)
if re.match('GPS GPSAltitudeRef', tag):
GPS['GPSAltitudeRef(高度标识)'] = str(value)
if re.match('GPS GPSLatitude', tag):
try:
match_result = re.match('\[(\w*),(\w*),(\w.*)/(\w.*)\]', str(value)).group()
print(match_result, type(match_result))
GPS['GPSLatitudeRef(纬度标识)'] = int(match_result[0]), int(match_result[1]), int(match_result[2])/int(match_result(3))
except:
GPS['GPSLatitudeRef(纬度标识)'] = str(value)
if re.match('GPS GPSLongitude', tag):
try:
match_result = re.match('\[(\w*),(\w*),(\w.*)/(\w.*)\]', str(value)).group()
print(match_result, type(match_result))
GPS['GPSLongitude(经度标识)'] = int(match_result[0], int(match_result[1], int(match_result[2]/match_result[3])))
except:
GPS['GPSLatitudeRef(经度标识)'] = str(value)
if re.match('GPS GPSAltitude', tag):
GPS['GPSAltitude(高度)'] = str(value)
if re.match('Image DateTime', tag):
date = str(value)
return {'GPS信息': GPS, '时间': date}
if __name__ == '__main__':
ji = FindGPSimage(r'C:\Users\Lenovo\Desktop\1.jpg')
网友评论