抽帧
# 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()
# 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:27 shenhshihao 阅读(18) 评论(0) 收藏 举报
浙公网安备 33010602011771号