手搓python图片格式转换png jpg jpeg ico bmp 互转

import customtkinter as ctk
import imageio
from tkinter import filedialog, messagebox
import os


def convert_image(input_path, output_path):
    """
    Convert an image from input_path to the format specified by output_path.
    """
    image = imageio.imread(input_path)
    imageio.imwrite(output_path, image)


def browse_input_file():
    """
    Open a file dialog to select the input image file.
    """
    file_path = filedialog.askopenfilename(filetypes=[("Image files", "*.png;*.jpg;*.jpeg;*.bmp;*.gif;*.tiff;*.ico")])
    if file_path:
        input_file_entry.delete(0, ctk.END)
        input_file_entry.insert(0, file_path)


def browse_output_file():
    """
    Open a file dialog to select the output file location and name.
    """
    file_path = filedialog.asksaveasfilename(defaultextension=".png",
                                             filetypes=[("Image files", "*.png;*.jpg;*.jpeg;*.bmp;*.gif;*.tiff;*.ico")])
    if file_path:
        output_file_entry.delete(0, ctk.END)
        output_file_entry.insert(0, file_path)


def start_conversion():
    """
    Start the image conversion based on user input.
    """
    input_path = input_file_entry.get()
    output_path = output_file_entry.get()
    input_format = input_format_var.get()
    output_format = output_format_var.get()

    if not input_path or not output_path:
        messagebox.showwarning("输入警告", "请选择输入文件和输出文件。")
        return

    # Ensure the output file has the correct extension
    if not output_path.lower().endswith(f".{output_format}"):
        output_path = f"{os.path.splitext(output_path)[0]}.{output_format}"

    try:
        convert_image(input_path, output_path)
        messagebox.showinfo("处理完成", "图像转换完成!")
    except Exception as e:
        messagebox.showerror("处理失败", f"处理过程中发生错误:{e}")


# Create the main window
ctk.set_appearance_mode("dark")  # Set dark mode
ctk.set_default_color_theme("blue")  # Set color theme

root = ctk.CTk()
root.title("图片格式转换工具")
root.geometry("680x250")

# Create UI elements
ctk.CTkLabel(root, text="选择输入文件:").grid(row=0, column=0, padx=10, pady=10, sticky="e")
input_file_entry = ctk.CTkEntry(root, width=400)
input_file_entry.grid(row=0, column=1, padx=10, pady=10)
ctk.CTkButton(root, text="浏览", command=browse_input_file).grid(row=0, column=2, padx=10, pady=10)

ctk.CTkLabel(root, text="选择输出文件:").grid(row=1, column=0, padx=10, pady=10, sticky="e")
output_file_entry = ctk.CTkEntry(root, width=400)
output_file_entry.grid(row=1, column=1, padx=10, pady=10)
ctk.CTkButton(root, text="浏览", command=browse_output_file).grid(row=1, column=2, padx=10, pady=10)

# Format selection
input_format_var = ctk.StringVar(value='png')
output_format_var = ctk.StringVar(value='png')

ctk.CTkLabel(root, text="选择输入格式:").grid(row=2, column=0, padx=10, pady=10, sticky="e")
input_format_menu = ctk.CTkOptionMenu(root, variable=input_format_var,
                                      values=['png', 'jpg', 'jpeg', 'bmp', 'gif', 'tiff', 'ico'])
input_format_menu.grid(row=2, column=1, padx=10, pady=10)

ctk.CTkLabel(root, text="选择输出格式:").grid(row=3, column=0, padx=10, pady=10, sticky="e")
output_format_menu = ctk.CTkOptionMenu(root, variable=output_format_var,
                                       values=['png', 'jpg', 'jpeg', 'bmp', 'gif', 'tiff', 'ico'])
output_format_menu.grid(row=3, column=1, padx=10, pady=10)

ctk.CTkButton(root, text="开始转换", command=start_conversion).grid(row=4, column=1, padx=10, pady=20)

# Start the GUI event loop
root.mainloop()

为了方便 我打包了exe 可以免安装执行

手搓python图片格式转换png jpg jpeg ico bmp 互转_第1张图片

下载地址 https://download.csdn.net/download/m0_38124502/89678213

也可免费获取 +【qq】群

你可能感兴趣的:(python)