按比例从文件夹中取一定数量图片
# -*- coding: utf-8 -*-
import os, random, shutil
def moveFile(fileDir):
pathDir = os.listdir(fileDir) # 取图片的原始路径
filenumber = len(pathDir)
rate = 0.75 # 自定义抽取图片的比例
picknumber = int(filenumber * rate) # 按照rate比例从文件夹中取一定数量图片
sample = random.sample(pathDir, picknumber) # 随机选取picknumber数量的样本图片
print (sample)
for name in sample:
shutil.move(fileDir + name, tarDir + name)
return
if __name__ == '__main__':
fileDir = "/mnt/NIvsCG/datasets/prcg_images/" # 源图片文件夹路径
tarDir = '/mnt/NIvsCG/datasets/train/' # 移动到新的文件夹路径
moveFile(fileDir)
随机选取一定数量的样本图片
import os, random, shutil
data_base_dir = "/mnt/NIvsCG/temp/train/" # 源图片文件夹路径
tarDir = "/mnt/NIvsCG/random/train/" # 移动到新的文件夹路径
for file in os.listdir(data_base_dir):
fileDir = data_base_dir + file + '/' # 取图片的原始路径
pathDir = os.listdir(fileDir)
filenumber = len(pathDir)
if filenumber > 30:
picknumber = 30 # 所取图片数量
sample = random.sample(pathDir, picknumber) # 随机选取picknumber数量的样本图片
print(sample)
for name in sample:
shutil.copy(fileDir + name, tarDir + name)
按比例从文件夹中取一定数量图片,并在移动到新文件夹之前重命名选取的图片
import os, random, shutil
def moveFile(fileDir):
pathDir = os.listdir(fileDir) # 取图片的原始路径
filenumber = len(pathDir)
rate = 0.75 # 自定义抽取图片的比例
picknumber = int(1000 * rate) # 按照rate比例从文件夹中取一定数量图片
sample = random.sample(pathDir, picknumber) # 随机选取picknumber数量的样本图片
count = 1
for namein sample:
shutil.move(fileDir + name, tarDir + name)
os.rename(os.path.join(tarDir, name), os.path.join(tarDir, str(count) + ".jpg"))
count += 1
return
if __name__ == '__main__':
fileDir = "/mnt/NIvsCG/datasets/mobilephone_img/" # 源图片文件夹路径
tarDir = "/mnt/NIvsCG/datasets/train/" # 移动到新的文件夹路径
moveFile(fileDir)