DMS转换为十进制与Python PIEXIF吗?



下面是我使用Python 3的脚本:

from print import print
from PIL import Image
import piexif
codec = 'ISO-8859-1'  # or latin-1
def exif_to_tag(exif_dict):
exif_tag_dict = {}
thumbnail = exif_dict.pop('thumbnail')
exif_tag_dict['thumbnail'] = thumbnail.decode(codec)
for ifd in exif_dict:
exif_tag_dict[ifd] = {}
for tag in exif_dict[ifd]:
try:
element = exif_dict[ifd][tag].decode(codec)
except AttributeError:
element = exif_dict[ifd][tag]
exif_tag_dict[ifd][piexif.TAGS[ifd][tag]["name"]] = element
return exif_tag_dict
def main():
filename = 'subb.jpeg'  # obviously one of your own pictures
im = Image.open(filename)
exif_dict = piexif.load(im.info.get('exif'))
exif_dict = exif_to_tag(exif_dict)
pprint(exif_dict['GPS'])
if __name__ == '__main__':
main()e here

输出是:

{'GPSDateStamp': '2022:09:04',
'GPSLatitude': ((43, 1), (35, 1), (28845, 1277)),
'GPSLatitudeRef': 'N',
'GPSLongitude': ((13, 1), (12, 1), (30645, 1024)),
'GPSLongitudeRef': 'E'}

嗯,问题是,我不知道如何将输出转换为十进制。那么,如何将其从十进制转换为十进制呢?谢谢你。

ESRI将DMS转换为DD的公式描述为:

十进制度=度+((分钟/60)+(秒/3600))

还要注意,根据方向的不同,你可能需要让你的数字为负:

度、分、秒后通常后跟N、S、E或w的半球标签。在转换为十进制度数时,将西半球的经度值或南半球的纬度值转换为负的十进制度数。

但在此之前,GPSLatitudeGPSLongitude被存储为3个有理数元组,每个都是(numerator, denominator)的格式,所以你必须做一些除法来得到值。

使用你的图像的EXIF GPS元数据,这里的代码可以计算DMS。

def dms_to_dd(gps_exif):
# convert the rational tuples by dividing each (numerator, denominator) pair
lat = [n/d for n, d in gps_exif['GPSLatitude']]
lon = [n/d for n, d in gps_exif['GPSLongitude']]
# now you have lat and lon, which are lists of [degrees, minutes, seconds]
# from the formula above
dd_lat = lat[0] + lat[1]/60 + lat[2]/3600
dd_lon = lon[0] + lon[1]/60 + lon[2]/3600
# if latitude ref is 'S', make latitude negative
if gps_exif['GPSLatitudeRef'] == 'S':
dd_lat = -dd_lat

# if longitude ref is 'W', make longitude negative
if gps_exif['GPSLongitudeRef'] == 'W':
dd_lon = -dd_lon
return (dd_lat, dd_lon)

if __name__ == '__main__':
coords = { 
'GPSDateStamp': '2022:09:04',
'GPSLatitude': ((43, 1), (35, 1), (28845, 1277)),
'GPSLatitudeRef': 'N',
'GPSLongitude': ((13, 1), (12, 1), (30645, 1024)),
'GPSLongitudeRef': 'E'
}
print(dms_to_dd(coords))

您的示例EXIF坐标返回:

(43.58960780475072, 13.20831298828125)

如果我没算错的话,应该是在意大利的某个地方。

最新更新