图片镜像与旋转工具

查看 89|回复 9
作者:Hf6824329   
[Python] 纯文本查看 复制代码import tkinter as tk
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  

图片, 文件

忘情的城市   

能360度任意旋转和预览就好了,系统自带的好像就有这90度旋转
lujkhua   

313版本,运行出错,如图
通过AI解决:
[Python] 纯文本查看 复制代码import tkinter as tk
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)
            
            # 检查输出文件格式并相应处理
            if output_path.lower().endswith(('.jpg', '.jpeg')):
                if processed_img.mode in ('RGBA', 'LA'):
                    # 如果图片有透明通道,转换为RGB
                    processed_img = processed_img.convert('RGB')
            
            # 保存图片
            processed_img.save(output_path)
            messagebox.showinfo("成功", "图片处理完成!")
            
        except Exception as e:
            messagebox.showerror("错误", f"处理图片时出错:{str(e)}")
if __name__ == "__main__":
    app = ImageFlipperGUI()
    app.window.mainloop()
dhwl9899   

试试看怎么样,谢谢大侠的分享。
luvlive   

感谢分享
xingdh   

python3.12下运行通过,感谢楼主。
zdwycxm   

处理图片常要用到的功能,用个小工具解决问题,值得点赞。
yiyepz   

非常好的工具,谢谢分享
rgyjc   

小工具
解决
大麻烦
daoye9988   

旋转的常用功能都有了
您需要登录后才可以回帖 登录 | 立即注册

返回顶部