数据集处理系列:分别针对单张图片及数据集尺寸变换python代码详解

数据集处理系列:分别针对单张图片及数据集尺寸变换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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值