给家里的老照片添加时间、地点信息,方便查找回忆

起因

现在手机拍照,自带时间和地点信息,查找起来非常的方便。但是家里的老照片如果想看只能翻出来了。可以把老照片扫描到电子版,但是如何增加时间和经纬度信息就没有什么好用的软件了。没办法,只能自己来,写了一个python应用。
在这里插入图片描述

代码如下

from PIL import Image, ImageTk
from piexif import load, dump, ImageIFD,GPSIFD
import tkinter as tk
from tkinter import filedialog, messagebox
import pyperclip

def open_file():
    print("打开文件 start")
    file_path = filedialog.askopenfilename(title='选择图片文件')
    # file_path = 'C:/Users/hahaha/Pictures/IMG_1089.JPG'
    print(file_path)
    img = Image.open(file_path)
    img = img.resize((400, 300))
    img = ImageTk.PhotoImage(img)
    lable_img_display.config(image=img)
    lable_img_display.image = img

    if file_path:
        print("打开文件:", file_path)
        # 打开图片
        image = Image.open(file_path)
        img_name_old_label.config(text=file_path)

        # 读取图片的原始exif信息'
        try:
            exif_dic = load(image.info["exif"])
            image_time = exif_dic["0th"][ImageIFD.DateTime]
            print(image_time)
            img_time_old_label.config(text=image_time)
            if exif_dic["GPS"]:
                longtitude = exif_dic['GPS'][GPSIFD.GPSLongitude] # 经度
                print(format_data(longtitude))
                img_longtitude_old_label.config(text=format_data(longtitude))
                latitude = exif_dic["GPS"][GPSIFD.GPSLatitude]
                print(format_data(latitude))
                img_latitude_old_label.config(text=format_data(latitude)) # 纬度
                print(longtitude)
                print(latitude)
                print(format_data(format_data(longtitude)))
            else:
                print("无GPS信息")
                img_longtitude_old_label.config(text="none")
                img_latitude_old_label.config(text="none")
                pass
        except:
            img_time_old_label.config(text="none")
            img_longtitude_old_label.config(text="none")
            img_latitude_old_label.config(text="none")


def format_data(latlong):
    print(type(latlong))
    if type(latlong) == float or type(latlong) == str or type(latlong) == int:
        latlong = float(latlong)
        degree = int(latlong)
        res_degree = latlong - degree
        minute = int(res_degree * 60)
        res_minute = res_degree * 60 - minute
        seconds = round(res_minute * 60, 3)

        _data = ((degree, 1), (minute, 1), (int(seconds * 100), 100))
        return _data
    elif type(latlong) == tuple:
        _date = latlong[0][0]+latlong[1][0]/60+latlong[2][0]/60/6000
        return _date


def save_file():
    file_path = filedialog.asksaveasfilename()
    if file_path:
        print("保存文件:", file_path)

def modify_exif():

    print('修改图片的exif信息')
    # 打开图片
    image_path = img_name_old_label.cget("text")
    image = Image.open(image_path)

    # 读取图片的原始exif信息
    try:
        exif_data = load(image.info["exif"])
    except:
        exif_data = {}

    if '0th' not in exif_data:  # 判断是否存在基础信息 并创建基础信息
        exif_data['0th'] = {}
    if len(exif_data['0th']) == 0:
        exif_data['0th'] = {
            271: b'QRJ', #制造商
            272: b'scanner',#型号
            # 274: 1, # 图像方向
            # 282: (72, 1), #ImageWidth 方向上每个 ResolutionUnit 的像素数
            # 283: (72, 1), #ImageLength方向上每个 ResolutionUnit 的像素数
            # 296: 2, #XResolution 和 YResolution 的测量单位。
            305: b'IMG_EDITv1', #用于创建映像的软件包的名称和版本号。
            306: b'2000:00:00 00:00:00', # 创建时间
            # 531: 1,# 指定子采样色度分量相对于亮度样本的位置。
            # 34665: 208,
            # 34853: 1786
        }
        exif_data['0th'][ImageIFD.DateTime] = img_time_new_text.get()  # 创建基础exif空字典

    if 'GPS' not in exif_data: # 判断是否存在基础GPS信息 并创建GPS基础信息
        exif_data['GPS'] = {} # 创建gps空字典
    if len(exif_data['GPS']) == 0:
        exif_data['GPS'] = {
            1: b'N', # 南北纬
            2: ((40, 1), (6, 1), (2386, 100)), # 纬度
            3: b'E', #东西经
            4: ((116, 1), (32, 1), (5287, 100)), # 经度
            # 5: 0, #表示作为参考高度的高度
            # 6: (13973, 415), #根据 GPSAltitudeRef 中的参考值指示高度
            # 7: ((4, 1), (5, 1), (4760, 100)), #表示时间为 UTC(协调世界时)。
            # 12: b'K', #表示用于表示 GPS 接收器移动速度的单位。
            # 13: (0, 1), #表示GPS接收器移动的速度
            # 16: b'T', #表示在捕获图像时给出图像方向的参考。
            # 17: (10159, 338), #表示图像拍摄时的方向。
            # 23: b'T', #表示用于给目标点方位的参考。
            # 24: (10159, 338), #表示到目的地的方位。
            # 29: b'2000:00:00', #一个字符串,记录相对于 UTC(协调世界时)的日期和时间信息。
            # 31: (65, 1) #指示是否对 GPS 接收器应用差分校正。
        }
        # if GPSIFD.GPSLongitude not in exif_data['GPS']:
#     exif_data['GPS'][GPSIFD.GPSLongitude] = []  # 创建gps空列表
        if img_longtitude_new_text.get() == '':
            messagebox.showinfo("提示", "请输入经度")
            return

        exif_data['GPS'][GPSIFD.GPSLongitude] = format_data(img_longtitude_new_text.get()) # 更新经度
# if GPSIFD.GPSLatitude not in exif_data['GPS']:
#     exif_data['GPS'][GPSIFD.GPSLatitude] = []
        if img_latitude_new_text.get() == '':
            messagebox.showinfo("提示", "请输入纬度")
            return
        exif_data['GPS'][GPSIFD.GPSLatitude] = format_data(img_latitude_new_text.get()) # 更新纬度




    ########################EXIF信息################################

    if 'Exif' not in exif_data:  # 判断是否存在Exif信息 并创建Exif信息
        exif_data['Exif'] = {}
    if len(exif_data['Exif']) == 0:
        exif_data['Exif'] = {
            # 33434: (1, 400),
            # 33437: (22, 10),
            # 34850: 3,
            # 34855: 100,
            # 34864: 2,
            # 34866: 100,
            # 36864: b'0230',
            36867: b'2022:10:30 17:01:01',
            # 36868: b'2022:10:30 17:02:02',
            # 37121: b'\x01\x02\x03\x00',
            # 37377: (565248, 65536),
            # 37378: (155648, 65536),
            # 37380: (0, 1),
            # 37383: 5,
            # 37385: 16,
            # 37386: (50, 1),
            # 42037: b'0000205042\x00'
        }
        exif_data['Exif'][36867] = img_time_new_text.get()  # 创建Exif时间信息

        #############Interop信息#######################################

        # if 'Interop' not in exif_data:
        #     exif_data['Interop'] = {}
        # if len(exif_data['Interop']) == 0:
        #     exif_data['Interop'] = {1: b'R98'}
        #


        #################1st##############
        # if '1st' not in exif_data:
        #     exif_data['1st'] = {}
        # if len(exif_data['1st']) == 0:
        #     exif_data['1st'] = {
        #         259: 6,
        #         282: (72, 1),
        #         283: (72, 1),
        #         296: 2,
        #         513: 8916,
        #         514: 14640
        #     }




    # 修改原始exif信息
    # exif_data.update(new_exif_dict)
    exif_bytes = dump(exif_data)

    # 设置新的exif信息
    image.save(image_path, exif=exif_bytes)






def paste_location():
    # TODO: 从剪贴板中粘贴经纬度
    gps_info=pyperclip.paste()
    print(gps_info)
    if ',' in gps_info:
        gps_info = gps_info.split(',')
        img_longtitude_new_text.insert(tk.END, gps_info[0])
        img_latitude_new_text.insert(tk.END, gps_info[1])

    pass

if __name__ == '__main__':
    # TODO: 实现GUI或者命令行接口,让用户选择操作
    root = tk.Tk()
    root.title("文件对话框")
    root.geometry("600x500")


    img_info_frame = tk.Frame(root)
    img_info_frame.pack(pady=20)
    img_name_label = tk.Label(img_info_frame, text="图片名称")
    img_name_label.grid(row=0, column=0)
    img_name_old_label = tk.Label(img_info_frame, text="原图片")
    img_name_old_label.grid(row=0, column=1)
    img_time_label = tk.Label(img_info_frame, text="图片时间")
    img_time_label.grid(row=1, column=0)
    img_time_old_label = tk.Label(img_info_frame, text="原图片时间")
    img_time_old_label.grid(row=1, column=1)
    img_time_new_text = tk.Entry(img_info_frame, width=20)
    img_time_new_text.grid(row=1, column=2)
    img_time_new_text.insert(tk.END, "2018:02:02 21:44:35")
    # img_time_new_text.config(state=tk.DISABLED)

    img_longitude_label = tk.Label(img_info_frame, text="经度")
    img_longitude_label.grid(row=2, column=0)
    img_longtitude_old_label = tk.Label(img_info_frame, text="原经度")
    img_longtitude_old_label.grid(row=2, column=1)
    img_longtitude_new_text = tk.Entry(img_info_frame, width=20)
    img_longtitude_new_text.grid(row=2, column=2)


    img_latitude_label = tk.Label(img_info_frame, text="纬度")
    img_latitude_label.grid(row=3, column=0)
    img_latitude_old_label = tk.Label(img_info_frame, text="原纬度")
    img_latitude_old_label.grid(row=3, column=1)
    img_latitude_new_text = tk.Entry(img_info_frame, width=20)
    img_latitude_new_text.grid(row=3, column=2)

    button_frame = tk.Frame(root,width=600,height=50)
    button_frame.pack()
    open_button = tk.Button(button_frame, text="打开单独文件", command=open_file)
    open_button.grid(row=4, column=2)

    save_button = tk.Button(button_frame, text="保存文件", command=save_file)
    save_button.grid(row=4, column=3)

    modify_button = tk.Button(button_frame, text="修改exif", command=modify_exif)
    modify_button.grid(row=4, column=4)

    paste_button = tk.Button(button_frame, text="粘贴经纬度", command=paste_location)
    paste_button.grid(row=4, column=5)

    website_text = tk.Text(root, width=54, height=1)
    website_text.pack()
    website_text.insert(tk.END, "https://api.map.baidu.com/lbsapi/getpoint/index.html")


    lable_img_display = tk.Label(root,width=400,height=300)
    lable_img_display.pack()

    #webview2 创建坐标浏览页面
    # web_view = tk.




    root.mainloop()

    pass
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
学校地图管理是指利用C语言编程添加、修改和查找学校地图相关信息的功能。 首先,添加功能可以让用户输入需要添加的学校地图相关信息,例如地点名称、地点描述、地点坐标等。通过使用C语言中的结构体来存储这些信息,将它们按照一定的规则添加到地图数据中。添加功能可以通过读取用户输入的信息,创建新的结构体对象,并将其添加到已有的地图数据中。 其次,修改功能可以允许用户更新学校地图信息。用户可以选择需要修改的地点,并输入新的地点信息。通过使用C语言中的指针操作,定位到指定的地点,并通过更新结构体成员的方式实现信息的修改。修改功能可以提供给用户一个交互界面,让用户选择需要修改的地点并进行相关操作。 然后,查找功能可以让用户根据特定的条件查询学校地图信息。用户可以输入关键词或指定位置坐标进行查询。通过使用C语言中的循环结构,遍历地图数据,对每一个地点进行判断,匹配用户输入的条件,然后输出相应的地点信息查找功能可以提供用户友好的交互界面,方便用户输入条件并显示查询结果。 在实现以上功能时,可以使用C语言提供的文件读写操作,将地图数据保存在文件中,以便程序启动时可以加载已有的地图信息。同时,要确保程序的健壮性,加入错误处理机制,例如输入非法参数时的提示和重新输入机制,以提高用户体验。 总结来说,学校地图管理功能的添加、修改和查找功能需要使用C语言的结构体、指针和循环等基本操作,结合文件读写和错误处理机制,实现用户友好的地图管理系统。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值