吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 397|回复: 7
收起左侧

[Python 原创] 照片位置信息修改器GPS经纬度

[复制链接]
junjiezi 发表于 2025-3-28 08:38
本帖最后由 junjiezi 于 2025-3-28 09:27 编辑

功能简单,如图:
录制_2025_03_27_15_53_31_658.gif
录制_2025_03_27_15_53_01_812.gif
修改经纬度的代码:
[Asm] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
import tkinter as tk
from tkinter import messagebox
from tkinterdnd2 import TkinterDnD, DND_FILES
from PIL import Image
from PIL.ExifTags import TAGS, GPSTAGS
import piexif
 
def degrees_to_dms(degrees):
    """将十进制度数转换为度分秒格式"""
    d = int(degrees)
    md = abs(degrees - d) * 60
    m = int(md)
    sd = (md - m) * 60
    return ((d, 1), (m, 1), (int(sd * 10000), 10000))
 
def update_gps_info(image_path, latitude, longitude):
    """更新图片的GPS信息"""
    try:
        # 尝试加载图片的EXIF数据
        exif_dict = piexif.load(image_path)
    except Exception:
        # 如果没有EXIF数据,则创建一个新的字典
        exif_dict = {"0th": {}, "Exif": {}, "GPS": {}, "1st": {}, "thumbnail": None}
 
    # 转换经纬度为度分秒格式
    lat_ref = "N" if latitude >= 0 else "S"
    lon_ref = "E" if longitude >= 0 else "W"
    latitude = abs(latitude)
    longitude = abs(longitude)
 
    gps_data = {
        piexif.GPSIFD.GPSLatitude: degrees_to_dms(latitude),
        piexif.GPSIFD.GPSLatitudeRef: lat_ref,
        piexif.GPSIFD.GPSLongitude: degrees_to_dms(longitude),
        piexif.GPSIFD.GPSLongitudeRef: lon_ref,
    }
 
    # 更新EXIF中的GPS信息
    exif_dict["GPS"] = gps_data
 
    # 保存修改后的EXIF数据到图片
    try:
        exif_bytes = piexif.dump(exif_dict)
        piexif.insert(exif_bytes, image_path)
        messagebox.showinfo("成功", "图片的GPS信息已更新!")
    except Exception as e:
        messagebox.showerror("错误", f"无法更新图片的GPS信息:{e}")
 
def on_drop(event):
    """处理拖放事件"""
    # 获取拖放的文件路径
    image_path = event.data.strip('{}')  # 去掉可能的花括号
    process_image(image_path)
 
def process_image(image_path):
    """处理图片并更新GPS信息"""
    try:
        # 获取用户输入的经纬度
        latitude = float(lat_entry.get())
        longitude = float(lon_entry.get())
    except ValueError:
        messagebox.showwarning("警告", "请输入有效的经纬度数值!")
        return
 
    # 更新图片的GPS信息
    update_gps_info(image_path, latitude, longitude)
 
# 创建主窗口
root = TkinterDnD.Tk()
root.title("图片GPS信息修改工具")
 
# 设置窗口大小
root.geometry("500x400")
 
# 创建一个容器用于放置经度和纬度的输入框
frame_coords = tk.Frame(root)
frame_coords.pack(pady=10)
 
# 经度标签和文本框
tk.Label(frame_coords, text="经度:").pack(side=tk.LEFT, padx=5)
lon_entry = tk.Entry(frame_coords, width=20)
lon_entry.pack(side=tk.LEFT, padx=5)
 
# 纬度标签和文本框
tk.Label(frame_coords, text="纬度:").pack(side=tk.LEFT, padx=5)
lat_entry = tk.Entry(frame_coords, width=20)
lat_entry.pack(side=tk.LEFT, padx=5)
 
# 拖放区域
drop_label = tk.Label(
    root,
    text="将图片拖放到此处",
    relief="solid",
    width=50,
    height=15,
    font=("Arial", 14)
)
drop_label.pack(pady=20)
 
# 绑定拖放事件
drop_label.drop_target_register(DND_FILES)  # 注册拖放目标
drop_label.dnd_bind("<<Drop>>", on_drop)   # 绑定拖放事件
 
# 运行主循环
root.mainloop()


获取经纬度的代码:
[Asm] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
import tkinter as tk
from tkinter import filedialog, messagebox
from tkinterdnd2 import TkinterDnD, DND_FILES  # 使用 tkinterdnd2 支持拖放
from PIL import Image
from PIL.ExifTags import TAGS, GPSTAGS
 
def get_exif_data(image_path):
    """获取图片的EXIF数据"""
    try:
        with Image.open(image_path) as img:
            exif_data = img._getexif()
            if not exif_data:
                return None
            return {TAGS.get(tag, tag): value for tag, value in exif_data.items()}
    except Exception as e:
        return None
 
def get_gps_info(exif_data):
    """从EXIF数据中提取GPS信息"""
    gps_info = {}
    if "GPSInfo" in exif_data:
        for key, value in exif_data["GPSInfo"].items():
            gps_info[GPSTAGS.get(key, key)] = value
        return gps_info
    return None
 
def convert_to_degrees(value):
    """将GPS坐标转换为十进制度数"""
    d, m, s = value
    return d + (m / 60.0) + (s / 3600.0)
 
def get_lat_lon(gps_info):
    """从GPS信息中提取经纬度"""
    latitude = longitude = None
    if "GPSLatitude" in gps_info and "GPSLatitudeRef" in gps_info:
        latitude = convert_to_degrees(gps_info["GPSLatitude"])
        if gps_info["GPSLatitudeRef"] != "N":
            latitude = -latitude
    if "GPSLongitude" in gps_info and "GPSLongitudeRef" in gps_info:
        longitude = convert_to_degrees(gps_info["GPSLongitude"])
        if gps_info["GPSLongitudeRef"] != "E":
            longitude = -longitude
    return latitude, longitude
 
def on_drop(event):
    """处理拖放事件"""
    # 获取拖放的文件路径
    image_path = event.data.strip('{}')  # 去掉可能的花括号
    process_image(image_path)
 
def process_image(image_path):
    """处理图片并显示经纬度"""
    exif_data = get_exif_data(image_path)
    if not exif_data:
        messagebox.showwarning("警告", "该图片没有EXIF信息")
        return
    gps_info = get_gps_info(exif_data)
    if not gps_info:
        messagebox.showwarning("警告", "未找到GPS信息")
        return
    latitude, longitude = get_lat_lon(gps_info)
    if latitude is None or longitude is None:
        messagebox.showwarning("警告", "无法解析经纬度信息")
        return
    # 更新文本框内容
    lon_entry.delete(0, tk.END)  # 先清空经度文本框
    lat_entry.delete(0, tk.END)  # 再清空纬度文本框
    lon_entry.insert(0, str(longitude))  # 插入经度
    lat_entry.insert(0, str(latitude))   # 插入纬度
 
# 创建主窗口(使用 TkinterDnD.Tk)
root = TkinterDnD.Tk()
root.title("图片经纬度提取工具")
 
# 设置窗口大小
root.geometry("500x400")
 
# 创建一个容器用于放置经度和纬度的输入框
frame_coords = tk.Frame(root)
frame_coords.pack(pady=10)
 
# 经度标签和文本框(调整到前面)
tk.Label(frame_coords, text="经度:").pack(side=tk.LEFT, padx=5)
lon_entry = tk.Entry(frame_coords, width=20)
lon_entry.pack(side=tk.LEFT, padx=5)
 
# 纬度标签和文本框(调整到后面)
tk.Label(frame_coords, text="纬度:").pack(side=tk.LEFT, padx=5)
lat_entry = tk.Entry(frame_coords, width=20)
lat_entry.pack(side=tk.LEFT, padx=5)
 
# 拖放区域(加大尺寸)
drop_label = tk.Label(root, text="将图片拖放到此处", relief="raised", width=40, height=15, font=("Arial", 18))
drop_label.pack(pady=1)
 
# 绑定拖放事件
drop_label.drop_target_register(DND_FILES)  # 注册拖放目标
drop_label.dnd_bind("<<Drop>>", on_drop)   # 绑定拖放事件
 
# 运行主循环
root.mainloop()



免费评分

参与人数 1吾爱币 +5 热心值 +1 收起 理由
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

linlin01 发表于 2025-3-30 00:12
是什么语言写出来的程序呢,
 楼主| junjiezi 发表于 2025-3-31 09:03
reginald 发表于 2025-3-31 10:42
下载链接呢? 有没有可以批量下载的 或者是否支持批量拖拽
Pablo 发表于 2025-3-31 11:03
原理是修改了图片里面的元数据吗
 楼主| junjiezi 发表于 2025-3-31 11:20
Pablo 发表于 2025-3-31 11:03
原理是修改了图片里面的元数据吗

是的,修改了图片里面的元数据,如果没有经纬度也可以添加经纬度
 楼主| junjiezi 发表于 2025-3-31 11:22
reginald 发表于 2025-3-31 10:42
下载链接呢? 有没有可以批量下载的 或者是否支持批量拖拽

下载链接之前发了,但是被这个网站删除了,我再发一个试试。
通过网盘分享的文件:Python相关
链接: https://pan.baidu.com/s/1TiIpyDBYuBaMWsLGVOKaZg?pwd=1234 提取码: 1234
reginald 发表于 2025-3-31 14:06
junjiezi 发表于 2025-3-31 11:22
下载链接之前发了,但是被这个网站删除了,我再发一个试试。
通过网盘分享的文件:Python相关
链接: ht ...

这次可以了 感谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-4-23 22:06

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表