写入元数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | # import piexif # from PIL import Image # # # def deg_to_dms_rational(deg_float): # "" " # 将十进制的度数转换为 EXIF 所需的 (度, 分, 秒) 的分数表示形式。 # "" " # # 取绝对值 # deg_float = abs(deg_float) # # 度 # degrees = int (deg_float) # # 分 # minutes = int ((deg_float - degrees) * 60 ) # # 秒(保留小数部分) # seconds = round((deg_float - degrees - minutes / 60 ) * 3600 , 5 ) # # # EXIF要求以分数表示,这里简单将秒乘以 1000 ,再以 (秒* 1000 , 1000 ) 表示秒的分数形式 # return ((degrees, 1 ), (minutes, 1 ), ( int (seconds * 1000 ), 1000 )) # # # def add_gps_exif(input_image_path, output_image_path, latitude, longitude): # "" " # 将指定的经纬度写入图片的EXIF GPS信息中,然后保存为新的图片文件。 # # 参数: # input_image_path: 原始图片路径 # output_image_path: 输出图片路径 # latitude: 纬度,正数表示北纬,负数表示南纬 # longitude: 经度,正数表示东经,负数表示西经 # "" " # # 根据经纬度确定参考方向 # lat_ref = "N" if latitude >= 0 else "S" # lon_ref = "E" if longitude >= 0 else "W" # # # 转换为 (度, 分, 秒) 格式的分数形式 # lat_dms = deg_to_dms_rational(latitude) # lon_dms = deg_to_dms_rational(longitude) # # # 构造 GPS IFD 字典 # gps_ifd = { # piexif.GPSIFD.GPSLatitudeRef: lat_ref, # piexif.GPSIFD.GPSLatitude: lat_dms, # piexif.GPSIFD.GPSLongitudeRef: lon_ref, # piexif.GPSIFD.GPSLongitude: lon_dms, # } # # # 如果原图片有EXIF数据,可以先加载,否则新建一个空字典 # try : # exif_dict = piexif.load(input_image_path) # except Exception: # exif_dict = { "0th" : {}, "Exif" : {}, "GPS" : {}, "1st" : {}, "thumbnail" : None} # # # 更新或添加GPS信息 # exif_dict[ "GPS" ] = gps_ifd # # 将字典转换为 bytes # exif_bytes = piexif.dump(exif_dict) # # # 打开图片,并保存到新文件中,同时写入EXIF数据 # image = Image.open(input_image_path) # image.save(output_image_path, "jpeg" , exif=exif_bytes) # print(f "已将GPS信息写入 {output_image_path}" ) # # # # 示例:给 input.jpg 写入北京的经纬度(纬度 39.9042 , 经度 116.4074 ),输出为 output.jpg # if __name__ == "__main__" : # input_path = "input.jpg" # 原始图片路径 # output_path = "output.jpg" # 输出图片路径 # lat = 39.9042 # lon = 116.4074 # add_gps_exif(input_path, output_path, lat, lon) # import re import os import piexif from PIL import Image def deg_to_dms_rational(deg_float): "" " 将十进制的经纬度转换为 EXIF 所需的 (度, 分, 秒) 的分数格式 "" " deg = abs(deg_float) degrees = int (deg) minutes = int ((deg - degrees) * 60 ) seconds = round((deg - degrees - minutes / 60 ) * 3600 , 5 ) # 用 (秒* 1000 , 1000 ) 表示秒的分数形式 return ((degrees, 1 ), (minutes, 1 ), ( int (seconds * 1000 ), 1000 )) def add_gps_exif(image_path, latitude, longitude): "" " 将给定的经纬度信息写入指定 JPEG 图片的 EXIF GPS 字段 "" " # 判断经纬度参考方向 lat_ref = "N" if latitude >= 0 else "S" lon_ref = "E" if longitude >= 0 else "W" # 转换为 (度, 分, 秒) 格式的分数 lat_dms = deg_to_dms_rational(latitude) lon_dms = deg_to_dms_rational(longitude) # 构造 GPS IFD 数据 gps_ifd = { piexif.GPSIFD.GPSLatitudeRef: lat_ref, piexif.GPSIFD.GPSLatitude: lat_dms, piexif.GPSIFD.GPSLongitudeRef: lon_ref, piexif.GPSIFD.GPSLongitude: lon_dms, } # 读取原图片的EXIF信息,如无则创建新的EXIF字典 try : exif_dict = piexif.load(image_path) except Exception: exif_dict = { "0th" : {}, "Exif" : {}, "GPS" : {}, "1st" : {}, "thumbnail" : None} # 更新GPS信息 exif_dict[ "GPS" ] = gps_ifd exif_bytes = piexif.dump(exif_dict) # 写入EXIF数据后保存图片(直接覆盖原文件,也可以另存为其他文件) img = Image.open(image_path) img.save(image_path, "jpeg" , exif=exif_bytes) print(f "已写入 {image_path}: latitude={latitude}, longitude={longitude}" ) def parse_srt_file(srt_file_path): "" " 从srt文件中提取每组记录的latitude和longitude 返回一个列表,每个元素为 (latitude, longitude) 的元组 "" " with open(srt_file_path, "r" , encoding= "utf-8" ) as f: content = f.read() # 使用正则表达式提取 [latitude: 数字] 与 [longitude: 数字] # 注意如果数字中有负号或小数点,也能匹配到 pattern = re.compile(r "\[latitude:\s*(-?\d+\.\d+)\].*?\[longitude:\s*(-?\d+\.\d+)\]" , re.DOTALL) matches = pattern.findall(content) gps_list = [] for lat_str, lon_str in matches: gps_list.append( ( float (lat_str), float (lon_str)) ) return gps_list def main(): # SRT文件路径(包含记录的文本) srt_file = "info.srt" # 存放JPEG图片的文件夹路径(图片文件名格式 frame_0001.jpg, frame_0002.jpg, ...) images_folder = "images" # 解析srt文件获取GPS数据列表 gps_data_list = parse_srt_file(srt_file) if not gps_data_list: print( "没有解析到GPS数据,请检查srt文件格式!" ) return # 按顺序依次将GPS数据写入对应的图片中 for idx, (latitude, longitude) in enumerate(gps_data_list, start= 1 ): # 构造图片文件名(假设文件名格式为 frame_0001.jpg, frame_0002.jpg, ...) image_filename = f "frame_{idx:04d}.jpg" image_path = os.path.join(images_folder, image_filename) if not os.path.exists(image_path): print(f "图片 {image_path} 不存在,跳过..." ) continue add_gps_exif(image_path, latitude, longitude) if __name__ == "__main__" : main() |
posted on 2025-02-01 14:41 shenhshihao 阅读(6) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」