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

50次阅读
没有评论

共计 1152 个字符,预计需要花费 3 分钟才能阅读完成。

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

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

pip install pillow
pip install exifread

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

使用 PIL 库:

from PIL import Image
from PIL.ExifTags import TAGS

def 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 exifread

def 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.’

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2024-02-05发表,共计1152字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)