Temp/Depth-Anything-V2-main/tk_name.py

162 lines
6.6 KiB
Python
Raw Normal View History

2025-07-03 13:48:55 +08:00
import tkinter as tk
from tkinter import ttk, filedialog, messagebox, simpledialog
import json
import os
from lighter import process_images
class NamingConfigApp:
def __init__(self, root):
self.root = root
self.root.title("图片命名配置工具")
# 配置数据存储
self.config_data = {}
self.current_frame = None
# 创建界面
self.create_widgets()
self.load_config() # 自动尝试加载已有配置
def create_widgets(self):
# 顶部按钮区域
top_frame = ttk.Frame(self.root, padding=10)
top_frame.pack(fill=tk.X)
# 配置显示区域
self.canvas = tk.Canvas(self.root)
self.scrollbar = ttk.Scrollbar(self.root, orient="vertical", command=self.canvas.yview)
self.scrollable_frame = ttk.Frame(self.canvas)
ttk.Label(self.scrollable_frame, text="配置项:").pack(side=tk.TOP, pady=10)
self.scrollable_frame.bind(
"<Configure>",
lambda e: self.canvas.configure(
scrollregion=self.canvas.bbox("all")
)
)
self.canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
# 右部操作区域
right_frame = ttk.Frame(self.root, padding=10)
right_frame.pack(fill=tk.X)
ttk.Button(right_frame, text="新建配置", command=self.create_new_config).pack(side=tk.BOTTOM, padx=5)
ttk.Button(right_frame, text="加载配置", command=self.load_config_dialog).pack(side=tk.BOTTOM, padx=5)
ttk.Button(right_frame, text="保存配置", command=self.save_config).pack(side=tk.BOTTOM, padx=5)
ttk.Button(right_frame, text="处理图片", command=self.process_images).pack(side=tk.TOP, padx=5)
# 底部提示区域
bottom_frame = ttk.Frame(self.root, padding=10)
bottom_frame.pack(fill=tk.Y)
ttk.Label(bottom_frame, text=
"注意:请在左侧配置区域进行配置。项目名称请大写缩写。\n"
"点击“处理图片”按钮进行图片处理。无新需求不用新建配置。\n"
"处理图片时请选择后面带#号的风机组和叶片号顶层文件夹。\n"
).pack(side=tk.BOTTOM, padx=5)#"新建配置后修改utils.get_name()函数中的规则即可"
def create_config_fields(self):
# 清空现有配置显示
if self.current_frame:
self.current_frame.destroy()
self.current_frame = ttk.Frame(self.scrollable_frame)
self.current_frame.pack(fill=tk.BOTH, expand=True)
# 动态生成配置项
for i, (title, value) in enumerate(self.config_data.items()):
row_frame = ttk.Frame(self.current_frame)
row_frame.grid(row=i, column=0, sticky="ew", padx=5, pady=2)
ttk.Label(row_frame, text=title, width=20).pack(side=tk.LEFT)
entry = ttk.Entry(row_frame)
entry.insert(0, value)
entry.pack(side=tk.RIGHT, fill=tk.X, expand=True)
# 保存引用以便后续获取值
self.config_data[title] = entry
def create_new_config(self):
# 创建新配置项对话框
title = simpledialog.askstring("新建配置项", "请输入配置项名称:")
if title:
if title in self.config_data:
messagebox.showerror("错误", "配置项已存在!")
return
self.config_data[title] = "" # 初始化空值
self.create_config_fields()
def load_config_dialog(self):
# 加载配置对话框
file_path = filedialog.askopenfilename(
initialdir=os.getcwd(),
title="选择配置文件",
filetypes=(("JSON文件", "*.json"),)
)
if file_path:
self.load_config(file_path)
def load_config(self, file_path="naming_config.json"):#naming_config.json
# 加载配置逻辑
try:
with open(file_path, "r") as f:
data = json.load(f)
self.config_data = {k: v for k, v in data.items()}
self.create_config_fields()
except FileNotFoundError:
messagebox.showinfo("信息", "未找到配置文件,请新建配置")
except Exception as e:
messagebox.showerror("错误", f"配置文件加载失败:{str(e)}")
def save_config(self):
# 保存配置逻辑
try:
# 获取所有输入框的值
final_config = {title: entry.get()
for title, entry in self.config_data.items()}
with open("naming_config.json", "w") as f:
json.dump(final_config, f, indent=2)
messagebox.showinfo("成功", "配置保存成功!")
except Exception as e:
messagebox.showerror("错误", f"保存失败:{str(e)}")
def process_images(self):
# 处理图片逻辑
path = filedialog.askdirectory(title="选择图片文件夹")
if not path:
return
config = {title: entry.get() for title, entry in self.config_data.items()}
# 这里调用实际的处理函数
self.actual_image_processor(path, config)
messagebox.showinfo("完成", f"已处理图片目录:{path}")
def actual_image_processor(self, path, config):
"""实际图片处理函数(需要根据需求实现)"""
print(f"处理路径:{path}")
print(f"当前配置:{config}")
input_dir=path
output_dir = input_dir+"+调整"
# if not os.path.exists(output_dir):
# os.makedirs(output_dir,exist_ok=True)
# gamma = float(input("请输入筛选阴影区阈值(0~255)建议120~200优先160若阴影亮度调高效果不明显可以调大值: "))
# threshold = float(input("请输入筛选爆光区阈值(0~255)建议240~254优先245若曝光区亮度降低不明显或区域较大可以调小: "))
gamma = 180
threshold = 253
no_auto_bright = False
process_images(input_dir, output_dir, gamma, threshold, no_auto_bright,config)
print("处理完成,输出路径:", output_dir)
# 在这里添加实际的图片处理逻辑
if __name__ == "__main__":
root = tk.Tk()
app = NamingConfigApp(root)
root.mainloop()