python图片格式批量转换


前言

图片格式批量转换,废话不多说,先来张图,选择需要批量转换的文件夹,确认需要转换的文件格式和要转换的文件格式,默认为jpg,点击一键转换 可批量进行图片格式的转换,转换后的文件存放在原目录中的output文件夹中,请确保,本身源文件夹中无此文件夹,避免与其余文件混乱。

打包好的工具已上传附件,下载打开即可使用,无需安装,windos操作系统64位,解压后,直接双击main.exe即可使用

在这里插入图片描述


`

一、环境

开发工具:PyCharm
开发语言:python 3.12
第三方工具包:tkinter、PIL等

二、使用步骤

1.引入库

代码如下(示例):

from tkinter import *
import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
import os
from PIL import Image

导入需要使用的第三方包

2.全局变量定义

代码如下(示例):

# 定义变量
choosePath = ""
transTypeList = [
        '.jpg', '.JPG', '.jpeg', '.JPEG',
        '.png', '.PNG', '.ppm', '.PPM', '.bmp', '.BMP', '.tiff'
    ]
transFrontType = transTypeList[0]
transEndType = transTypeList[0]

定义全局变量已选择的文件夹,可供选择的图片格式集合,选择的转换前后格式类型。


3.前端展示

window = Tk() # 创建窗口对象
window.title("图片格式转换工具") # 设置窗口标题
window.geometry("1280x720") # 设置窗口大小
# 第一行选择一个文件夹
b = tk.Button(window, text='格式转换选择文件夹', font=('Arial', 12), width=20, height=1, command=tempCreate)
b.place(x=20,y=20,width=200,height=50)
tk.Label(window, text="文件夹路径:").place(x=250,y=20,width=100,height=50)

# 第二行
tk.Label(window, text="从").place(x=20,y=100,width=50)
def abnorSeleFront (event):
    global transFrontType
    transFrontType = transTypeList[abnorBox.current()]
abnorBox = ttk.Combobox(window,values=transTypeList,width=20,state="readonly")
abnorBox.set(transTypeList[0]) # 设置默认选项
abnorBox.place(x=80,y=100,width=50)
abnorBox.bind("<<ComboboxSelected>>", abnorSeleFront)

tk.Label(window, text="转为").place(x=140,y=100,width=50)

def abnorSeleEnd (event):
    global transEndType
    transEndType = transTypeList[abnorBox.current()]
abnorBox = ttk.Combobox(window,values=transTypeList,width=20,state="readonly")
abnorBox.set(transTypeList[0]) # 设置默认选项
abnorBox.place(x=200,y=100,width=50)
abnorBox.bind("<<ComboboxSelected>>", abnorSeleEnd)


b = tk.Button(window, text='一键转换', font=('Arial', 12),bg="red", width=20, height=1, command=startTrans)
b.place(x=50,y=150,width=100)

window.mainloop() # 运行窗口主循环

窗口创建,默认大小可根据屏幕调整。通过 place布局格式进行位置确定排版,并保存选择数据

3.图片格式转换逻辑

读取当前选择的文件夹,循环遍历所有符合已选择格式的文件,进行循环转换输出,输出文件夹为源文件夹+output的目录下

# 点击转换
def startTrans():
    outputPath = choosePath + "/output"
    print("当前值为:",transFrontType,transEndType,choosePath,outputPath)
    # 确保输出文件夹存在
    if not os.path.exists(outputPath):
        os.makedirs(outputPath)
    # 遍历文件夹中的所有文件
    for filename in os.listdir(choosePath):
        if filename.endswith(transFrontType):
            # 读取图片
            img_path = os.path.join(choosePath, filename)
            img = Image.open(img_path)
            # resized_image = img.resize((120, 120), Image.ANTIALIAS)

            # 修改文件拓展名为.png
            new_filename = filename[:-4] + transEndType
            new_img_path = os.path.join(outputPath, new_filename)

            # 保存图片为png格式
            print("值",transEndType[-3:])
            img.save(new_img_path, transEndType[-3:])

总结

简单工具使用,未添加必要校验,可根据要求调整修改。已经打包好的exe工具,下载既可使用,操作环境为64位

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值