![](/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.