溫馨提示×

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

小億
233
2024-01-30 15:56:49
欄目: 編程語言

要獲取照片的地理定位信息,可以使用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.’

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女