python实现快速文件格式批量转换的方法
作者:itinerary,hui 时间:2021-10-31 22:53:12
用python实现文件夹下的成批文件格式转换
我们对于文件转换的需求很大,甚至于对于图片的格式,JPG和PNG格式在肉眼看来都没什么差别,但是对于计算机而言,它有时候就只接受这些肉眼看起来差不多的格式的其中一种。
环境
windows10
python3.7+pycharm
创建目录
1.在编程前,创建一个文件夹,并放入你想用的文件(非目录),这些文件的格式不合适。
例如,我在桌面创建了名为"in_path"的文件夹,在里面放进了.pgm和.png格式的文件,想让他们都转化成.jpg格式。
2.同时新建一个batch_change.py文件。
编写程序
导入python的模块os,PIL,glob
.
// 导入PIL,os,glob
from PIL import Image
import os,glob
创建输出目录
// 创建输出文件夹
def batch_change(in_path,out_path):
if not os.path.exists(out_path):
print(out_path,'is not existed.')
os.mkdir(out_path)
if not os.path.exists(in_path):
print(in_path,'is not existed.')
return -1
浏览输入目录
// 浏览遍历输入文件夹
for files in glob.glob(in_path+'/*'):
filepath,filename=os.path.split(files)
out_file = filename[0:9]+'.jpg' #转换成最终格式为.jpg,可以在这里改为.png
im = Image.open(files)
new_path=os.path.join(out_path,out_file)
print(count,',',new_path)
count = count+1
im.save(os.path.join(out_path,out_file))
修改文件路径
// 浏览遍历输入文件夹
if __name__=='__main__':
batch_change(r'C:\Users\80610\Desktop\in_path',r'C:\Users\80610\Desktop\out_path')
#你想转化文件所在文件夹输入和输出的路径
运行结果
无论是pgm,png,他们们都转化成.jpg格式,并且保存在out_path文件夹下
完整代码
#encoding = utf-8
#author = itinerary,hui
from PIL import Image
import os,glob
def batch_change(in_path,out_path): #参数:输入与输出文件夹路径
if not os.path.exists(out_path):
print(out_path,'is not existed.')
#创建输出文件夹
os.mkdir(out_path)
if not os.path.exists(in_path):
print(in_path,'is not existed.')
return -1
count = 0
for files in glob.glob(in_path+'/*'):
filepath,filename=os.path.split(files)
out_file = filename[0:9]+'.png' #转换成最终格式为png
im = Image.open(files)
new_path=os.path.join(out_path,out_file)
print(count,',',new_path)
count = count+1
im.save(os.path.join(out_path,out_file))
if __name__=='__main__':
batch_change(r'C:\Users\80610\Desktop\in_path',r'C:\Users\80610\Desktop\out_path') #你想转化文件所在文件夹输入和输出的路近
总结
来源:https://blog.csdn.net/weixin_50683750/article/details/109110483
标签:python,格式,批量转换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于JSON以及JSON在PHP中的应用技巧
2023-11-16 00:03:38
pyTorch深入学习梯度和Linear Regression实现
2023-05-12 01:00:45
![](https://img.aspxhome.com/file/2023/5/89435_0s.png)
python实现支付宝当面付(扫码支付)功能
2023-04-01 21:38:45
python连接kafka加载数据的项目实践
2021-04-23 07:14:38
![](https://img.aspxhome.com/file/2023/2/89382_0s.png)
python实例化对象的具体方法
2023-11-05 21:52:11
python中通过pip安装库文件时出现“EnvironmentError: [WinError 5] 拒绝访问”的问题及解决方案
2023-08-29 20:25:40
浅谈python抛出异常、自定义异常, 传递异常
2022-12-22 00:49:31
三分钟python搭建支付宝三方支付
2022-02-03 22:58:42
![](https://img.aspxhome.com/file/2023/0/95890_0s.jpg)
微信小程序上传图片功能(附后端代码)
2023-07-24 04:21:40
![](https://img.aspxhome.com/file/2023/9/55969_0s.jpg)
设计72变——寻求banner制作的变化
2009-11-12 12:56:00
![](https://img.aspxhome.com/file/UploadPic/200911/12/t11bvnxfhjxxxxxxxx-600-95-92s.jpg)
教你学会通过python的matplotlib库绘图
2022-03-04 13:08:52
![](https://img.aspxhome.com/file/2023/5/78605_0s.png)
使用批处理实现mysql的数据库备份与上传
2012-01-05 19:30:23
python的字典和集合你了解吗
2022-12-13 11:28:44
![](https://img.aspxhome.com/file/2023/0/65550_0s.png)
python绘制简单直方图的方法
2023-06-23 19:54:50
![](https://img.aspxhome.com/file/2023/3/89323_0s.jpg)
使用 Supervisor 监控 Python3 进程方式
2021-06-21 11:48:13
![](https://img.aspxhome.com/file/2023/2/90422_0s.jpg)
Python实现的数据结构与算法之基本搜索详解
2021-07-25 03:47:44
![](https://img.aspxhome.com/file/2023/4/67934_0s.png)
浅谈哪个Python库才最适合做数据可视化
2022-12-05 00:34:58
![](https://img.aspxhome.com/file/2023/1/97351_0s.png)
如何拒绝同一张表单被多次提交?
2009-12-16 18:46:00
用Django实现一个可运行的区块链应用
2022-07-17 22:26:31
![](https://img.aspxhome.com/file/2023/3/101323_0s.jpg)
Python lambda表达式原理及用法解析
2021-03-02 18:52:12