标注_开始菜单.png (419.23 KB, 下载次数: 0)
下载附件
效果图
2024-12-3 10:37 上传
开始菜单.png (416.2 KB, 下载次数: 0)
下载附件
原图
2024-12-3 10:37 上传
软件界面
QQ20241203-103705.png (93.56 KB, 下载次数: 0)
下载附件
软件界面
2024-12-3 10:38 上传
完整代码如下[Python] 纯文本查看 复制代码import os
from tkinter import Tk, filedialog, messagebox, scrolledtext
from tkinter.ttk import Frame, Label, Entry, Button
from PIL import Image, ImageDraw, ImageFont
import tkinter as tk
class SettingsApp:
def __init__(self, master):
self.master = master
master.title("框图_快速在图片中标注图片文件名")
self.border_height_var = tk.DoubleVar(value=0.05)
self.font_size_var = tk.DoubleVar(value=0.04)
self.prefix_var = tk.StringVar(value="标注_")
# 初始化路径变量
self.image_paths = [] # 用于存储选中的多个图片路径
self.output_folder = "" # 存储输出文件夹路径
self.create_widgets()
def create_widgets(self):
frame = Frame(self.master, padding="20")
frame.grid(row=0, column=0, sticky="NSEW")
Label(frame, text="底框大小 %:").grid(row=0, column=0)
Entry(frame, textvariable=self.border_height_var).grid(row=0, column=1)
Label(frame, text="字体大小 %:").grid(row=1, column=0)
Entry(frame, textvariable=self.font_size_var).grid(row=1, column=1)
Label(frame, text="新文件前缀:").grid(row=2, column=0)
Entry(frame, textvariable=self.prefix_var).grid(row=2, column=1)
Button(frame, text="选择图片", command=self.select_images).grid(row=3, column=0)
Button(frame, text="选择保存的文件夹", command=self.choose_output_folder).grid(row=3, column=1)
# 使用ScrolledText widget显示多选的文件路径
self.image_paths_display = scrolledtext.ScrolledText(frame, height=5, wrap=tk.WORD)
self.image_paths_display.grid(row=4, column=0, columnspan=2, sticky="WE")
self.image_paths_display.configure(state='disabled')
Button(frame, text="开始处理图片", command=self.process_images).grid(row=6, column=0, columnspan=2)
for child in frame.winfo_children():
child.grid_configure(padx=5, pady=5)
def select_images(self):
file_paths = filedialog.askopenfilenames(title="选择需处理的图片", filetypes=[("Image Files", "*.jpg *.jpeg *.png *.gif")])
if file_paths:
self.image_paths.extend(file_paths)
paths_str = "\n".join(self.image_paths)
self.image_paths_display.configure(state='normal')
self.image_paths_display.delete(1.0, tk.END)
self.image_paths_display.insert(tk.END, paths_str)
self.image_paths_display.configure(state='disabled')
def choose_output_folder(self):
self.output_folder = filedialog.askdirectory()
def process_images(self):
if not self.image_paths or not self.output_folder:
messagebox.showerror("错误", "请选择图片保存的文件夹.")
return
border_percent = self.border_height_var.get()
font_size = self.font_size_var.get()
prefix = self.prefix_var.get()
success_count = 0
error_files = []
for image_path in self.image_paths:
try:
filename = os.path.basename(image_path)
output_path = os.path.join(self.output_folder, f"{prefix}{filename}")
add_border_and_text(image_path, output_path, border_percent, font_size, prefix)
success_count += 1
except Exception as e:
error_files.append((filename, str(e)))
if success_count > 0:
if len(error_files) == 0:
messagebox.showinfo("成功", f"所有图片已成功处理并保存至: {self.output_folder}")
else:
error_message = "\n".join([f"文件: {file}, 错误: {error}" for file, error in error_files])
messagebox.showinfo("完成但有错误", f"成功处理了 {success_count} 张图片并保存至: {self.output_folder}\n\n处理中遇到以下错误:\n{error_message}")
else:
if error_files:
error_message = "\n".join([f"文件: {file}, 错误: {error}" for file, error in error_files])
messagebox.showerror("处理失败", f"图片处理失败,请重试。\n错误详情:\n{error_message}")
else:
messagebox.showerror("处理失败", "未处理任何图片,请检查设置并重试。")
def add_border_and_text(image_path, output_path, border_percent, font_size, prefix):
img = Image.open(image_path)
width, height = img.size
border_height = int(height * border_percent)
new_height = height + border_height
new_image = Image.new('RGB', (width, new_height), 'white')
new_image.paste(img, (0, 0))
font_path = r"C:\Windows\Fonts\simsun.ttc" # 确保该字体存在或替换为其他字体路径
font = ImageFont.truetype(font_path, int(height * font_size))
filename = os.path.splitext(os.path.basename(image_path))[0]
text_width, text_height = ImageDraw.Draw(new_image).textsize(filename, font=font)
text_position = ((width - text_width) // 2, height + (border_height - text_height) // 2)
draw = ImageDraw.Draw(new_image)
draw.text(text_position, f"{filename}", fill='black', font=font)
new_image.save(output_path)
if __name__ == "__main__":
root = Tk()
app = SettingsApp(root)
root.mainloop()