Created
December 1, 2018 16:21
-
-
Save miracleyoo/d54668d5f12d473edfad352361cd1110 to your computer and use it in GitHub Desktop.
[random_choice_files_in_batch] #python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import os | |
from shutil import copyfile | |
ori_path = './your-original-path' | |
out_path = './your-output-path' | |
dirs = [os.path.join(ori_path, adir) for adir in os.listdir(ori_path) if not adir.startswith('.')] | |
pure_dirs = [adir for adir in os.listdir(ori_path) if not adir.startswith('.')] | |
for adir, pure_adir in zip(dirs, pure_dirs): | |
print("==> Now processing:", adir) | |
atype = adir.split('/')[-1].split('-')[0] | |
print(atype) | |
files = [os.path.join(adir, afile) for afile in os.listdir(adir) if not afile.startswith('.')] | |
files = random.choices(files, k=500) | |
for i, afile in enumerate(files): | |
prefix = os.path.join(out_path, atype) | |
if not os.path.exists(prefix): os.mkdir(prefix) | |
new_name = os.path.join(prefix, str(i) + '.jpg') | |
copyfile(afile, new_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment