用Python将Exif DMS转换为DD地理位置

5 投票
2 回答
3757 浏览
提问于 2025-04-16 20:10

我正在使用以下代码来提取用iPhone拍摄的图片的地理位置:

from PIL import Image
from PIL.ExifTags import TAGS

def get_exif(fn):
    ret = {}
    i = Image.open(fn)
    info = i._getexif()
    for tag, value in info.items():
        decoded = TAGS.get(tag, tag)
        ret[decoded] = value
    return ret

a = get_exif('photo2.jpg')
print a

这是我得到的结果:

    {
    'YResolution': (4718592, 65536),
    41986: 0,
    41987: 0,
    41990: 0,
    'Make': 'Apple',
    'Flash': 32,
    'ResolutionUnit': 2,
    'GPSInfo': {
        1: 'N',
        2: ((32, 1), (4571, 100), (0, 1)),
        3: 'W',
        4: ((117, 1), (878, 100), (0, 1)),
        7: ((21, 1), (47, 1), (3712, 100))
    },
    'MeteringMode': 1,
    'XResolution': (4718592, 65536),
    'ExposureProgram': 2,
    'ColorSpace': 1,
    'ExifImageWidth': 1600,
    'DateTimeDigitized': '2011:03:01 13:47:39',
    'ApertureValue': (4281, 1441),
    316: 'Mac OS X 10.6.6',
    'SensingMethod': 2,
    'FNumber': (14, 5),
    'DateTimeOriginal': '2011:03:01 13:47:39',
    'ComponentsConfiguration': '\x01\x02\x03\x00',
    'ExifOffset': 254,
    'ExifImageHeight': 1200,
    'Model': 'iPhone 3G',
    'DateTime': '2011:03:03 10:37:32',
    'Software': 'QuickTime 7.6.6',
    'Orientation': 1,
    'FlashPixVersion': '0100',
    'YCbCrPositioning': 1,
    'ExifVersion': '0220'
}

所以,我想知道如何把GPS信息中的值(DMS格式)转换成十进制度数,以便得到实际的坐标?另外,结果中似乎有两个“西”的标记……?

2 个回答

1

你可以去这个链接查看内容:http://www.exiv2.org/tags.html,在页面中找到字符串 'Exif.GPSInfo.GPSLatitude'。 在这里,你会看到3对数字(表示分数),其中第二个数字是分母。 我本来以为在经度后面应该是高度,但实际上更符合的是GPS时间戳。

在这个例子中:

32/1 + (4571 / 100)/60 + (0 / 1)/3600 = 32.761833 N
117/1 + (878 / 100)/60 + (0 / 1)/3600 = 117.146333 W

这张照片是在加州圣地亚哥的4646 Park Blvd附近拍的吗?如果不是,那就忽略这个回答。

11

这里有一种方法,是我几个月前用pyexiv2写的脚本改编而来的:

a = get_exif('photo2.jpg')
lat = [float(x)/float(y) for x, y in a['GPSInfo'][2]]
latref = a['GPSInfo'][1]
lon = [float(x)/float(y) for x, y in a['GPSInfo'][4]]
lonref = a['GPSInfo'][3]

lat = lat[0] + lat[1]/60 + lat[2]/3600
lon = lon[0] + lon[1]/60 + lon[2]/3600
if latref == 'S':
    lat = -lat
if lonref == 'W':
    lon = -lon

这段代码给了我你照片的经纬度:32.7618333, -117.146333(和Lance Lee的结果一样)。

GPSInfo的最后一项可能是照片的朝向。你可以使用一些工具,比如exiv2或exiftools,来查看不同EXIF值的具体名称,以确认这一点。

撰写回答