leslievan / semi-utils

一个批量添加相机机型和拍摄参数的工具,后续「可能」添加其他功能。
https://lsvm.xyz/2023/02/semi-utils-intro/
Apache License 2.0
1.3k stars 130 forks source link

可以再加上图片的经纬度,用于替换原本的的时间(附参考代码) #83

Closed yiboLi1006 closed 9 months ago

yiboLi1006 commented 9 months ago

可以再加上图片的经纬度,替换默认的时间

参考代码:

import exifread
def get_exif_exifread(image_path):
    # 输入图片路径,获取exif
    exif = {}
    with open(image_path, 'rb') as f:
        tags = exifread.process_file(f)
        for tag, value in tags.items():
            if tag not in ('JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'EXIF MakerNote'):
                exif[tag] = str(value)
    return exif

def convert_dms(d, m, s):
    # 单位换算
    dig = d + m / 60 + s / 3600
    return dig

def Get_location(file_path):
    # 获得经纬度
    with open(file_path, 'rb') as f:  # 以二进制加载图像
        # 返回字典,包含EXIF信息
        tags = exifread.process_file(f)
        gps_latitude = str(tags.get("GPS GPSLatitude")).split(',')
        d = int(gps_latitude[0].replace('[', ''))
        m = float(eval(gps_latitude[1]))
        s = float(eval(gps_latitude[2].replace(']', '')))
        latitude = convert_dms(d, m, s)
        gps_longitude = str(tags.get("GPS GPSLongitude")).split(',')
        d = int(gps_longitude[0].replace('[', ''))
        m = float(eval(gps_longitude[1]))
        s = float(eval(gps_longitude[2].replace(']', '')))
        longitude = convert_dms(d, m, s)
    return longitude, latitude

print(Get_location('test.jpg'))  # 将获得元组形式的坐标

---------------------------------------------------------------------------
>>> (118.1107, 24.4303)
leslievan commented 9 months ago

已经加上了