from tkinter import filedialog, messagebox
from PIL import Image
import os
class ImageFlipperGUI:
def __init__(self):
self.window = tk.Tk()
self.window.title("图片翻转与旋转工具")
self.window.geometry("400x400")
# 输入文件路径
self.input_path = tk.StringVar()
tk.Label(self.window, text="输入图片:").pack(pady=5)
tk.Entry(self.window, textvariable=self.input_path, width=40).pack()
tk.Button(self.window, text="选择文件", command=self.select_input_file).pack(pady=5)
# 输出文件路径
self.output_path = tk.StringVar()
tk.Label(self.window, text="输出位置:").pack(pady=5)
tk.Entry(self.window, textvariable=self.output_path, width=40).pack()
tk.Button(self.window, text="选择位置", command=self.select_output_file).pack(pady=5)
# 操作选项
self.operation_type = tk.StringVar(value="horizontal")
tk.Label(self.window, text="操作方式:").pack(pady=5)
# 翻转选项
flip_frame = tk.Frame(self.window)
flip_frame.pack()
tk.Radiobutton(flip_frame, text="水平翻转", variable=self.operation_type, value="horizontal").pack(side=tk.LEFT)
tk.Radiobutton(flip_frame, text="垂直翻转", variable=self.operation_type, value="vertical").pack(side=tk.LEFT)
# 旋转选项
rotate_frame = tk.Frame(self.window)
rotate_frame.pack()
tk.Radiobutton(rotate_frame, text="向左旋转", variable=self.operation_type, value="rotate_left").pack(side=tk.LEFT)
tk.Radiobutton(rotate_frame, text="向右旋转", variable=self.operation_type, value="rotate_right").pack(side=tk.LEFT)
# 执行按钮
tk.Button(self.window, text="开始处理", command=self.process_image).pack(pady=20)
def select_input_file(self):
file_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg *.jpeg *.png *.bmp *.gif")])
if file_path:
self.input_path.set(file_path)
# 默认设置相同目录作为输出
default_output = os.path.join(os.path.dirname(file_path),
"processed_" + os.path.basename(file_path))
self.output_path.set(default_output)
def select_output_file(self):
file_path = filedialog.asksaveasfilename(
defaultextension=".png",
filetypes=[("PNG files", "*.png"), ("JPEG files", "*.jpg"), ("All files", "*.*")]
)
if file_path:
self.output_path.set(file_path)
def process_image(self):
input_path = self.input_path.get()
output_path = self.output_path.get()
if not input_path or not output_path:
messagebox.showerror("错误", "请选择输入和输出文件路径")
return
try:
# 打开图片
img = Image.open(input_path)
# 根据选择执行操作
operation = self.operation_type.get()
if operation == "horizontal":
processed_img = img.transpose(Image.FLIP_LEFT_RIGHT)
elif operation == "vertical":
processed_img = img.transpose(Image.FLIP_TOP_BOTTOM)
elif operation == "rotate_left":
processed_img = img.rotate(90, expand=True)
elif operation == "rotate_right":
processed_img = img.rotate(-90, expand=True)
# 保存图片
processed_img.save(output_path)
messagebox.showinfo("成功", "图片处理完成!")
except Exception as e:
messagebox.showerror("错误", f"处理图片时出错:{str(e)}")
if __name__ == "__main__":
app = ImageFlipperGUI()
app.window.mainloop()

Snipaste_2025-08-27_18-19-47.png (15.44 KB, 下载次数: 2)
下载附件
2025-8-27 18:19 上传
通过网盘分享的文件:图片镜像与旋转工具.exe
链接: https://pan.baidu.com/s/1gBocuWnCxFByo1Mhq_Gt7Q?pwd=hzhz 提取码: hzhz