在python中解释来自照片的exif数据的GPS信息 [英] Interpreting GPS info of exif data from photo in python
问题描述
我使用的库是Python中的PIL。现在我可以得到GPSInfo,就像这样:
{1:'N',
2 :((1,1,1),(20,1),(5365,100)),
3:'E',
4 :((103,1),(41,1), (1051,100)),
5:0,
6:(43,1),
7:((15,1),(32,1),(7,1 )),
16:'T',
17:(77473,452),
29:'2013:10:25'}
我怎样解释这个?而且我注意到标签不是连续的,那么是否有任何作弊单可供参考,以更好地了解所有数字标签及其含义?谢谢!
更新
对不起,我已经知道了。在PIL库中,有一个GPSTAGS.get()函数可以帮助我解码GPS信息中的密钥。谢谢!
gpsinfo = {}
用于exif ['GPSInfo']中的键。
decode = ExifTags.GPSTAGS.get(key,key)
gpsinfo [decode] = exif ['GPSInfo'] [key]
print gpsinfo
这里是结果
{' GPSTimeStamp':((15,1),(32,1),(7,1)),
'GPSImgDirectionRef':'T',
'GPSImgDirection':(77473,452),
'GPSLongitude':((103,1),(41,1),(1052,100)),
'GPSLatitudeRef':'N',29:'2013:10:25',
'GPSAltitude':(43,1),
'GPSLatitude':((1,1),(20,1),(5365,100)),
'GPSLongitudeRef':' E',
'GPSAltitudeRef':0}
使用exifread模块。
这是一个非常有用的 gist
导入exifread as ef
#barrowed from
#https ://gist.github.com/snakeye/fdc372dbf11370fe29eb
def _convert_to_degress(value):
帮助函数将存储在EXIF中的GPS坐标转换为浮点数格式的数值
:参数值:
:类型值:exifread.utils.Ratio
:rtype:float
d = float(value.values [0] .num )/ float(value.values [0] .den)
m = float(value.values [1] .num)/ float(value.values [1] .den)
s = float(value。值[2] .num)/ float(value.values [2] .den)
返回d +(m / 60.0)+(s / 3600.0)
def getGPS(filepath):
'''
返回gps数据(如果存在)其他明智返回空字典
'''
with open(filepath,'r')作为f:
tags = ef.process_file(f)
latitude = tags.get('GPS GPSLatitude')
latitude_ref = tags.get('GPS GPSLatitudeRef')
longitude = tags.get('GPS GPSLongitude')
longitude_ref = tags.get('GPS GPSLongitudeRef')
如果纬度:
lat_value = _convert_to_degress(纬度)
如果latitude_ref.values!='N':
lat_value = -lat_value
else:
return {}
如果经度:
lon_value = _convert_to_degress(经度)
如果longitude_ref.values!='E' :
lon_value = -lon_value
else:
return {}
return {'latitude':lat_value,'longitude':lon_value}
return {}
file_path ='文件的文件路径'
gps = getGPS(file_path)
print gps
I am writing a small program to get the GPS info of a iphone jpg photo.
The library I am using is the PIL in python. Now I am able to get the GPSInfo, which is something like:
{1: 'N',
2: ((1, 1), (20, 1), (5365, 100)),
3: 'E',
4: ((103, 1), (41, 1), (1052, 100)),
5: 0,
6: (43, 1),
7: ((15, 1), (32, 1), (7, 1)),
16: 'T',
17: (77473, 452),
29: '2013:10:25'}
How can I interpret this? And I notice the tag is not continuous, so is there any cheating sheet which I can refer to in order to get a better understanding of all the number tags and what they mean? Thank you!
UPDATES
Sorry, I have figured it out. In the PIL lib, there is a GPSTAGS.get() function which can help me decode the key in gps info. Thank you guys!
gpsinfo = {}
for key in exif['GPSInfo'].keys():
decode = ExifTags.GPSTAGS.get(key,key)
gpsinfo[decode] = exif['GPSInfo'][key]
print gpsinfo
and here is the result
{'GPSTimeStamp': ((15, 1), (32, 1), (7, 1)),
'GPSImgDirectionRef': 'T',
'GPSImgDirection': (77473, 452),
'GPSLongitude': ((103, 1), (41, 1), (1052, 100)),
'GPSLatitudeRef': 'N', 29: '2013:10:25',
'GPSAltitude': (43, 1),
'GPSLatitude': ((1, 1), (20, 1), (5365, 100)),
'GPSLongitudeRef': 'E',
'GPSAltitudeRef': 0}
Use exifread module.
Here is a very helpful gist
import exifread as ef
# barrowed from
# https://gist.github.com/snakeye/fdc372dbf11370fe29eb
def _convert_to_degress(value):
"""
Helper function to convert the GPS coordinates stored in the EXIF to degress in float format
:param value:
:type value: exifread.utils.Ratio
:rtype: float
"""
d = float(value.values[0].num) / float(value.values[0].den)
m = float(value.values[1].num) / float(value.values[1].den)
s = float(value.values[2].num) / float(value.values[2].den)
return d + (m / 60.0) + (s / 3600.0)
def getGPS(filepath):
'''
returns gps data if present other wise returns empty dictionary
'''
with open(filepath, 'r') as f:
tags = ef.process_file(f)
latitude = tags.get('GPS GPSLatitude')
latitude_ref = tags.get('GPS GPSLatitudeRef')
longitude = tags.get('GPS GPSLongitude')
longitude_ref = tags.get('GPS GPSLongitudeRef')
if latitude:
lat_value = _convert_to_degress(latitude)
if latitude_ref.values != 'N':
lat_value = -lat_value
else:
return {}
if longitude:
lon_value = _convert_to_degress(longitude)
if longitude_ref.values != 'E':
lon_value = -lon_value
else:
return {}
return {'latitude': lat_value, 'longitude': lon_value}
return {}
file_path = 'file path of the file'
gps = getGPS(file_path)
print gps
这篇关于在python中解释来自照片的exif数据的GPS信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!