python读取相片的经纬度信息
时间: 2023-06-28 17:13:03 浏览: 156
可以使用Pillow库和piexif库来读取图片的经纬度信息。以下是一个示例代码:
```python
from PIL import Image
import piexif
# 打开图片文件
image = Image.open('example.jpg')
# 获取图片的 Exif 信息
exif_dict = piexif.load(image.info["exif"])
# 获取经纬度信息
gps_ifd = exif_dict['GPS']
lat = gps_ifd[piexif.GPSIFD.GPSLatitude]
lon = gps_ifd[piexif.GPSIFD.GPSLongitude]
# 将经纬度转换为度分秒格式
def to_ddmmss(coord):
degrees = coord[0][0] / float(coord[0][1])
minutes = coord[1][0] / float(coord[1][1])
seconds = coord[2][0] / float(coord[2][1])
return degrees + (minutes / 60.0) + (seconds / 3600.0)
lat_dd = to_ddmmss(lat)
lon_dd = to_ddmmss(lon)
print("经度:", lon_dd)
print("纬度:", lat_dd)
```
这个示例代码假设图片的 Exif 信息中包含有 GPS 信息,如果没有则会抛出 KeyError 异常。如果要读取其他 Exif 信息,可以通过查询 piexif 文档来获取相关信息。
阅读全文
相关推荐


















