纯搞笑的,但是很好笑。
import tkinter as tk from tkinter import ttk, filedialog, messagebox from PIL import Image, ImageTk class ImageSymmetryApp: def __init__(self, root): self.root = root self.root.title("图片对称处理工具") # 初始化变量 self.original_image = None self.processed_image = None self.display_width = 800 # 预览区最大宽度 self.display_height = 600 # 预览区最大高度 # 创建界面组件 self.create_widgets() def create_widgets(self): # 控制面板 control_frame = ttk.Frame(self.root) control_frame.pack(padx=10, pady=10, fill=tk.X) # 文件选择按钮 ttk.Button( control_frame, text="选择图片", command=self.load_image ).grid(row=0, column=0, padx=5) # 方向选择下拉框 self.side_var = tk.StringVar(value="left") ttk.Combobox( control_frame, textvariable=self.side_var, values=["left", "right"], state="readonly", width=8 ).grid(row=0, column=1, padx=5) # 处理按钮 ttk.Button( control_frame, text="生成对称图", command=self.process_image ).grid(row=0, column=2, padx=5) # 保存按钮 ttk.Button( control_frame, text="保存结果", command=self.save_image ).grid(row=0, column=3, padx=5) # 图片预览区域 self.preview_label = ttk.Label(self.root) self.preview_label.pack(padx=10, pady=10) def load_image(self): file_path = filedialog.askopenfilename( filetypes=[("图片文件", "*.jpg;*.jpeg;*.png;*.bmp")] ) if file_path: try: self.original_image = Image.open(file_path) self.show_preview(self.original_image) except Exception as e: messagebox.showerror("错误", f"无法打开图片:\n{str(e)}") def process_image(self): if not self.original_image: messagebox.showwarning("提示", "请先选择图片") return selected_side = self.side_var.get() width, height = self.original_image.size mid = width // 2 # 根据选择的侧边进行处理 if selected_side == "left": left_part = self.original_image.crop((0, 0, mid, height)) mirrored = left_part.transpose(Image.FLIP_LEFT_RIGHT) self.processed_image = Image.new("RGB", (width, height)) self.processed_image.paste(left_part, (0, 0)) self.processed_image.paste(mirrored, (mid, 0)) else: right_part = self.original_image.crop((mid, 0, width, height)) mirrored = right_part.transpose(Image.FLIP_LEFT_RIGHT) self.processed_image = Image.new("RGB", (width, height)) self.processed_image.paste(mirrored, (0, 0)) self.processed_image.paste(right_part, (mid, 0)) self.show_preview(self.processed_image) def show_preview(self, image): # 缩放图片适应预览区域 img = image.copy() img.thumbnail((self.display_width, self.display_height)) photo = ImageTk.PhotoImage(img) # 更新预览 self.preview_label.configure(image=photo) self.preview_label.image = photo # 保持引用 def save_image(self): if not self.processed_image: messagebox.showwarning("提示", "没有可保存的图片") return save_path = filedialog.asksaveasfilename( defaultextension=".png", filetypes=[ ("PNG 文件", "*.png"), ("JPEG 文件", "*.jpg"), ("所有文件", "*.*") ] ) if save_path: try: self.processed_image.save(save_path) messagebox.showinfo("成功", "图片保存成功!") except Exception as e: messagebox.showerror("错误", f"保存失败:\n{str(e)}") if __name__ == "__main__": root = tk.Tk() app = ImageSymmetryApp(root) root.mainloop()