前面转自:https://www.jianshu.com/p/2777a74c7985
按比例从文件夹中取一定数量图片
# -*- 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)
最后按照上述方法写了自己想要的,按比例抽取文件夹内不同分辨率的图片,并移动到新的文件夹中
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 30 16:16:02 2019
@author: Administrator
"""
import math
import os, random, shutil
import os.path as osp
from scipy import misc
import collections
data_base_dir = "D:/Cargo" # 源图片文件夹路径
tarDir = "D:/Cargo_randomselect" # 移动到新的文件夹路径
if not osp.exists(tarDir):
os.mkdir(tarDir)
resolution_list=[]
resolution=[]
filelist = os.listdir(data_base_dir)
for file in filelist:
if file.endswith('tif'):
fileDir = data_base_dir + '/' + file # 取图片的路径
picshape = misc.imread(fileDir).shape
resolution_list.append(str(picshape[0])+'-'+file)
resolution.append(picshape[0])
statistical_result = collections.Counter(resolution)
for _, res in enumerate(statistical_result.keys()):
temp=[]
for tif in resolution_list:
if tif.split('-')[0] == str(res):
temp.append(tif.split('-')[1])
if res< 30:
sample = temp
elif res>30 and res<42:
picknumber = math.ceil(0.6 * statistical_result[res])
sample = random.sample(temp, picknumber)
elif res>42:
picknumber = math.ceil(0.25 * statistical_result[res])
sample = random.sample(temp, picknumber)
for name in sample:
src = data_base_dir + '/' + name#待复制文件路径
dst = tarDir+ '/' + name#目标路径
shutil.copy(src,dst)
print('selecting resolution {} picture'.format(res))