Python怎么获取照片的地理定位信息

   2024-11-08 8610
核心提示:要获取照片的地理定位信息,可以使用Python的PIL库(Python Imaging Library)或ExifRead库。首先,安装PIL库和ExifRead库可以使

要获取照片的地理定位信息,可以使用Python的PIL库(Python Imaging Library)或ExifRead库。

首先,安装PIL库和ExifRead库可以使用以下命令:

pip install pillowpip install exifread

接下来,使用以下代码可以获取照片的地理定位信息:

使用PIL库:

from PIL import Imagefrom PIL.ExifTags import TAGSdef get_geolocation(image_path):    image = Image.open(image_path)    exif_data = image._getexif()        if exif_data is not None:        for tag_id, value in exif_data.itEMS():            tag_name = TAGS.get(tag_id, tag_id)            if tag_name == 'GPSInfo':                return value    return None# 传入照片路径作为参数geolocation = get_geolocation('path/to/photo.jpg')if geolocation is not None:    print(geolocation)else:    print('No geolocation found.')

使用ExifRead库:

import exifreaddef get_geolocation(image_path):    with open(image_path, 'rb') as image_file:        tags = exifread.process_file(image_file)                if 'GPS GPSLatitude' in tags and 'GPS GPSLongitude' in tags:            latitude = tags['GPS GPSLatitude'].values            longitude = tags['GPS GPSLongitude'].values            return (latitude, longitude)    return None# 传入照片路径作为参数geolocation = get_geolocation('path/to/photo.jpg')if geolocation is not None:    print(geolocation)else:    print('No geolocation found.')

这些代码将会输出照片的地理定位信息,如果照片中不存在地理定位信息,则输出’No geolocation found.’

 
举报打赏
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行

网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策网站留言    |  赣ICP备2021007278号