import subprocess
import os
dir_gpg = r"C:\Users\Administrator\Desktop\photo"
def read_gps(img_path):
cmd = [
r"D:\exiftool.exe",
"-GPSLatitude#",
"-GPSLongitude#",
"-GPSAltitude#",
"-s3",
img_path
]
res = subprocess.run(cmd, capture_output=True, text=True, encoding="utf-8")
lines = res.stdout.splitlines()
if not lines:
return None
else:
if len(lines) == 1:
return None
elif len(lines) == 2:
lat = lines[0]
lon = lines[1]
alt = None
else:
lat = lines[0]
lon = lines[1]
alt = lines[2]
return [lon, lat, alt]
def get_all_jpg(input_path: str):
gps_list = []
failed_file_list = []
for name in os.listdir(input_path):
if name.lower().endswith((".jpg", ".jpeg")):
full_path = os.path.join(input_path, name)
gps_info = read_gps(full_path)
if gps_info:
if gps_info[2] is not None:
line = name + "," + gps_info[0] + "," + gps_info[1] + "," + gps_info[2]
else:
line = name + "," + gps_info[0] + "," + gps_info[1]
gps_list.append(line)
print("{} 的GPS信息读取成功".format(name))
else:
print("{} 的GPS信息读取失败".format(name))
failed_file_list.append(name)
else:
continue
txt_path = os.path.join(dir_gpg, "gps_info.txt")
if len(gps_list) > 0:
with open(txt_path, "w") as f:
for line in gps_list:
f.write(line + "\n")
txt_path = os.path.join(dir_gpg, "failed.txt")
if len(failed_file_list) > 0:
with open(txt_path, "w") as f:
for line in failed_file_list:
f.write(line + "\n")
get_all_jpg(dir_gpg)
import subprocess
import os
txt_file = r"C:\Users\Administrator\Desktop\GPS.txt"
dir_jpg = r"C:\Users\Administrator\Desktop\photo"
def remove_gps(input_path: str):
cmd = [
r"D:\exiftool.exe",
"-GPS*=",
"-overwrite_original",
"-P",
input_path
]
res = subprocess.run(cmd, capture_output=True, text=True)
def add_gps(input_path: str, longitude: float, latitude: float, altitude: float = None):
# 首先,删除图片的GPS信息
remove_gps(input_path)
cmd = [
r"D:\exiftool.exe",
f"-GPSLatitude={latitude}",
f"-GPSLongitude={longitude}",
"-GPSLatitudeRef=N" if latitude >= 0 else "-GPSLatitudeRef=S",
"-GPSLongitudeRef=E" if longitude >= 0 else "-GPSLongitudeRef=W",
"-overwrite_original",
"-P",
input_path
]
if altitude is not None:
cmd.insert(1, f"-GPSAltitude={altitude}")
cmd.insert(2, "-GPSAltitudeRef=0")
res = subprocess.run(cmd, capture_output=True, text=True)
if res.returncode == 0:
print(r"已写入 {} 的GPS信息".format(input_path))
else:
print(r"写入 {} 的GPS信息时出错,请核实".format(input_path))
def read_txt(txt_path: str):
with open(txt_path, "r") as f:
a = f.readlines()
list_gps = []
for line in a:
line = line.strip()
if line:
list_gps.append(line)
return list_gps
def write_gpsinfo(dir_path: str, txt_path: str):
info = read_txt(txt_path)
for item in info:
item_split = item.split(",")
jpg_name = item_split[0]
lon = float(item_split[1])
lat = float(item_split[2])
if len(item_split) > 3:
alt = float(item_split[3])
else:
alt = None
jpg_path = os.path.join(dir_path, jpg_name)
if os.path.exists(jpg_path):
add_gps(jpg_path, lon, lat, alt)
else:
print("{}不存在".format(jpg_path))
write_gpsinfo(dir_jpg, txt_file)
import subprocess
import os
dir_to_clear_gps = r"C:\Users\Administrator\Desktop\photo"
def remove_gps(input_path: str):
cmd = [
r"D:\exiftool.exe",
"-GPS*=",
"-overwrite_original",
"-P",
input_path
]
res = subprocess.run(cmd, capture_output=True, text=True)
if res.returncode == 0:
print("已清空 {} 的GPS信息".format(input_path))
else:
print("清空 {} 的GPS信息失败,请核实".format(input_path))
def get_all_jpg(input_path: str):
file_list = []
for name in os.listdir(input_path):
if name.lower().endswith((".jpg", ".jpeg")):
full_path = os.path.join(input_path, name)
file_list.append(full_path)
else:
continue
return file_list
# 清空照片的GSP信息时,保留以下信息,修改时只能修改引号内路径
jpgs = get_all_jpg(dir_to_clear_gps)
for item in jpgs:
remove_gps(item)
https://pan.baidu.com/s/1B4JYz_eQRbOTaHPKVTVu_g?pwd=63vy 提取码:63vy