在 python 中解释来自照片的 exif 数据的 GPS 信息

新手上路,请多包涵

我正在编写一个小程序来获取 iphone jpg 照片的 GPS 信息。

我使用的库是python中的PIL。现在我可以获取 GPSInfo,它类似于:

 {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'}

我该如何解释呢?我注意到标签不是连续的,那么有没有我可以参考的备忘单,以便更好地理解所有数字标签及其含义?谢谢!

更新

抱歉,我已经想通了。在 PIL 库中,有一个 GPSTAGS.get() 函数可以帮助我解码 gps 信息中的密钥。感谢你们!

 gpsinfo = {}
for key in exif['GPSInfo'].keys():
    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}

原文由 fyr91 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 547
1 个回答

使用 exifread 模块。

这是一个非常有用的 要点

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, 'rb') 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




原文由 Venkatesh Mondi 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题