You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python给EXIF加GPS坐标:Windows10不识别及优化方案咨询

问题:实现无需模板图片、且让Windows 10识别GPS坐标的JPG元数据修改方案

我在Windows和Ubuntu双系统复用脚本,选用exif库处理EXIF读写,编写了给JPG图片添加经纬度的函数(代码如下)。但exif库无法给无对应元数据槽位的图片添加坐标,因此采用了模板图片元数据的临时方案。目前添加后的坐标可被pic2map等网站识别,但Windows 10企业版的图片属性仅显示“海拔0”,看不到经纬度信息。现寻求无需模板图片、且能让Windows 10正常识别GPS坐标的解决方案。模板图片的经纬度在Windows属性中可正常显示,期望处理后的图片达到同样效果。

import exif
import numpy as np

def dec_to_dms(dec):
    '''
    Convert decimal degrees to degrees-minutes-seconds
    
    Parameters
    ----------
    dec : float
        Input coordinate in decimal degrees.
    
    Returns
    -------
    list
        Coordinate in degrees-minutes-seconds.
    '''
    degree = np.floor(dec)
    minutes = dec % 1.0 * 60
    seconds = minutes % 1.0 * 60
    minutes = np.floor(minutes)
    
    return [degree, minutes, seconds]

def add_coords(path, coordinates, replace = False):
    '''
    Add coordinates to the Exif data of a .jpg file.

    Parameters
    ----------
    path : str
        Full path to the image file.
    coordinates : list or tuple of float
        Latitude and longitude that shall be added to the image file.
    replace : bool
        Replace existing coordinates if the image alread contains values for
        the repective Exif tags.

    Returns
    -------
    None.
    '''
    with open(path, "rb") as f:
        
        img = exif.Image(f)
    
    lat = None
    
    if img.has_exif:
        if "gps_latitude" in img.list_all():
            lat = img.gps_latitude
            lon = img.gps_longitude
            
            ### While theoretically valid coordinates, (0.0, 0.0, 0.0) will be
            ### replaced since apparently, for some images (0.0, 0.0, 0.0) is
            ### set when no coordinates were specified.
            if lat == (0.0, 0.0, 0.0) or lon == (0.0, 0.0, 0.0):
                lat = lon = None
    
    if lat is None or replace:
        lat = tuple(dec_to_dms(coordinates[0]))
        lon = tuple(dec_to_dms(coordinates[1]))
        
        try:
            img.gps_latitude = lat
            img.gps_longitude = lon
        
        except:
            ###---------------------------------------------------------------|
            ### This is a quick and dirty workaround for current shortcomings
            ### of the exif package
            from PIL import Image
            EXPLIMG = "D:/switchdrive/PlantApp/img/Species/EXAMPLE_ANDROID.jpg"
            example_image = Image.open(EXPLIMG)
            example_exif = example_image.getexif()
            example_image.close()
            
            print(path)
            
            with Image.open(path) as current_image:
                current_image.save(path, exif = example_exif)
            
            with open(path, "rb") as f:
                img = exif.Image(f)
            
            img.gps_latitude = lat
            img.gps_longitude = lon
            ###---------------------------------------------------------------|
        
        with open(path, "wb") as f:
            
            f.write(img.get_file())
    
    return

内容的提问来源于stack exchange,提问作者Manuel Popp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 20:07:00