![](/img/trans.png)
[英]Convert DD (decimal degrees) to DMS (degrees minutes seconds) in Python?
[英]Convert DMS to Decimal with PIEXIF on Python?
这是我正在使用 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'}
十进制度数 = 度数 + ((分 / 60) + (秒 / 3600))
另请注意,根据方向,您可能需要将数字设为负数:
度分秒通常后跟 N、S、E 或 W 的半球标签。转换为十进制度时,将西半球的经度值或南半球的纬度值转换为负十进制度值。
但在此之前, GPSLatitude
和GPSLongitude
存储为 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)
如果我的数学是正确的,那是在意大利的某个地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.