手搓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 可以免安装执行

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

也可免费获取 +【qq】群

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值