数据集处理系列:分别针对单张图片及数据集尺寸变换python代码详解
✌针对单张图片做尺寸变换
代码如下
import os
import os.path
from PIL import Image
'''
filein: 输入图片
fileout: 输出图片
width: 输出图片宽度
height:输出图片高度
'''
def ResizeImage(filein, fileout, width, height):
img = Image.open(filein)
out = img.resize((width, height),Image.ANTIALIAS) #resize image with high-quality
out.save(fileout)
if __name__ == "__main__":
filein = r'E:\pytorch\STM-master\data_kitti\JPEGImages\480p\car-turn\00000.jpg'
fileout = r'E:\pytorch\STM-master\data_kitti\testout.png'
width = 1000
height = 400
ResizeImage(filein, fileout, width, height)
✌针对一个文件夹做尺寸变换
import os
import os.path
from PIL import Image
import cv2
'''
filein: 输入图片文件夹
fileout: 输出图片文件夹
width: 输出图片宽度
height:输出图片高度
type:输出图片类型(png, gif, jpeg...)
'''
def ResizeImage(filein, fileout, width, height, type):
for image in os.listdir(filein):
fullFile = os.path.join(filein, image)
img = Image.open(fullFile)
img = img.resize((width, height),Image.ANTIALIAS)
name, extension = os.path.splitext(image)
name= os.path.join(name+'.' +type)
print(name)
img.save(fileout+os.sep+name)
if __name__ == "__main__":
filein = r'C:\Users\TomTom\Desktop\car-turn11'
fileout = r'C:\Users\TomTom\Desktop\car-turn12'
width = 500
height = 400
type = 'png'
ResizeImage(filein, fileout, width, height, type)
👍集体更名
'''
只需要更改 file: 文件路径 keyWord: 需要修改的文件中所包含的关键字
'''
import os ,os.path ,time
def rename(file,keyword):
start =time.clock()
os.chdir(file)
items = os.listdir(file)
print(os.getcwd())
for name in items :
print(name)
# 遍历所有文件
if not os.path.isdir(name):
if keyword in name :
new_name = name.replace(keyword,'')
os.renames(name,new_name)
else:
rename(file + '\\' + name, keyword)
os.chdir('...')
print('-----------------------分界线------------------------')
items = os.listdir(file)
for name in items:
print(name)
rename('E:\\pytorch\\STM-master\\data_kitti\\JPEGImages\\480p\\car\\data', '00000')
👌批量修改文件格式方法
https://zhidao.baidu.com/question/2053787934750139107.html