python实现批量图片格式转换
作者:山水之间2018 时间:2021-07-15 16:07:42
本文实例为大家分享了python实现批量格式转换的具体代码,供大家参考,具体内容如下
深度学习过程中总是绕不开数据集的制作,有时候实际图片格式或大小可能与需要关心的图片信息不一致,那么我们只能手动做好数据预处理,再进行training dataset.现在将介绍最简单的格式转换问题。可以支持批量图片任意格式转换。
直接上代码:
# 将jpg格式转位png
import os
from PIL import Image
import shutil
import sys
# Define the input and output image
output_dirHR = '../data/Mosaic_HR/'
output_dirLR = '../data/Mosaic_LR/'
if not os.path.exists(output_dirHR):
os.mkdir(output_dirHR)
if not os.path.exists(output_dirLR):
os.mkdir(output_dirLR)
def image2png(dataset_dir,type):
files = []
image_list = os.listdir(dataset_dir)
files = [os.path.join(dataset_dir, _) for _ in image_list]
for index,jpg in enumerate(files):
if index > 100000:
break
try:
sys.stdout.write('\r>>Converting image %d/100000 ' % (index))
sys.stdout.flush()
im = Image.open(jpg)
png = os.path.splitext(jpg)[0] + "." + type
im.save(png)
# 将已经转换的图片移动到指定位置
'''''
if jpg.split('.')[-1] == 'jpg':
shutil.move(png,output_dirLR)
else:
shutil.move(png,output_dirHR)
'''
shutil.move(png, output_dirHR)
except IOError as e:
print('could not read:',jpg)
print('error:',e)
print('skip it\n')
sys.stdout.write('Convert Over!\n')
sys.stdout.flush()
if __name__ == "__main__":
current_dir = os.getcwd()
print(current_dir) # /Users/gavin/PycharmProjects/pygame
data_dir = '/home/gavin/MyProject/python/nesunai_faces/'
image2png(data_dir,'png')
来源:https://blog.csdn.net/gavinmiaoc/article/details/80448128
标签:python,格式转换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python 基于win32com客户端实现Excel操作的详细过程
2021-01-18 11:22:47
使用django实现一个代码发布系统
2022-06-28 02:42:45
![](https://img.aspxhome.com/file/2023/4/65114_0s.png)
精细讲述SQL Server数据库备份多种方法
2009-01-13 13:33:00
基于Swoole实现PHP与websocket聊天室
2023-11-03 19:04:05
![](https://img.aspxhome.com/file/2023/3/60393_0s.png)
asp精妙的SQL语句例子
2008-03-04 17:42:00
smarty简单分页的实现方法
2023-11-21 08:59:48
利用types增强vscode中js代码提示功能详解
2023-07-15 05:57:08
![](https://img.aspxhome.com/file/2023/1/55981_0s.png)
详解用python自制微信机器人,定时发送天气预报
2023-05-12 14:27:29
![](https://img.aspxhome.com/file/2023/8/76958_0s.png)
Django中自定义模型管理器(Manager)及方法
2022-12-01 17:53:05
Python求解平方根的方法
2023-02-13 13:25:47
python套接字流重定向实例汇总
2022-04-15 07:53:41
python 爬取小说并下载的示例
2023-08-24 19:31:49
![](https://img.aspxhome.com/file/2023/2/61962_0s.gif)
mysql3升级到mysql5解决乱码心得
2009-04-20 14:41:00
设计师和美工
2008-10-27 13:43:00
![](https://img.aspxhome.com/file/UploadPic/200810/27/2008102713452199s.jpg)
我的ImageMagick使用心得
2008-10-21 11:05:00
![](https://img.aspxhome.com/file/UploadPic/200810/21/20081021122020655s.jpg)
Python文件和流(实例讲解)
2023-08-02 19:11:45
AJAX实例:根据邮编自动完成地址信息
2008-03-06 19:44:00
MYSQL的DATE_FORMAT()格式化日期
2009-02-27 16:04:00
SQL Server数据库连接中常见的错误分析
2009-01-15 12:51:00
Python 中制作偶数列表的方法
2022-08-20 20:15:56
![](https://img.aspxhome.com/file/2023/8/63928_0s.webp)