from glob import glob
import numpy as np
import cv2
import os
select_num = 10
a = 'bmx-all'
if not os.path.exists('./images/reselect-%s' % a):
os.makedirs('./images/reselect-%s' % a)
path = glob('./datasets/DIV/%s/*' % a)
batch_images = np.random.choice(path, size=select_num)
i = 0
for img_path in batch_images:
img = cv2.imread(img_path)
cv2.imwrite('./images/reselect-%s/%s' % (a, i) + '.png', img)
i += 1
随机选取目录下若干个文件
最新推荐文章于 2024-06-16 07:09:52 发布
本文介绍了一个使用Python编写的脚本,该脚本能够从指定文件夹中随机选择一定数量的图片并将其复制到新的文件夹中。此脚本利用了Python标准库中的多个模块,包括glob用于获取文件路径列表,numpy用于生成随机数,cv2用于读取和写入图片,os用于创建目录。
1286

被折叠的 条评论
为什么被折叠?



