将随机选择的图像放入新文件夹

Placing randomly selected images into new folder

我有一个包含大量子目录的目录。

在每个子目录中都有不同的 jpeg、png。

我想:

Select 这些子目录中 X 数量的随机图像

创建一个新文件夹并将这些 selected 随机图像复制到里面。

感谢这里收到的帮助,我可以使用 os.walkrandom.choice 随机打印出 selection 图像。

import os
import random
import shutil


files_list = []
for root, dirs, files in os.walk("/Path/to/Directory"):
    for file in files:
        #all 
        if file.endswith(".jpg") or file.endswith(".png") or file.endswith(".jpeg"):
            files_list.append(os.path.join(root, file))


#print images
#lets me count and print the amount of jpeg,jpg,pmg 
file_count = len(files_list)
print file_count

print files_list   
print(random.sample(files_list, 2))  #prints two random files from list 

但是,我的问题是实际上 selecting 随机图像(不是它们的名字)

我试图创建一个使用 os.walk

的变量 imagePath
#creates a variable imagePath that lets me access all img files in different folders
imagePath = os.walk("/Path/to/Directory") 

和一个新变量,用于随机 select 来自 imagePath

的单个图像
#create a variable that lets me choose random iamge from imagePath
randomImages = random.choice(os.listdir(imagePath)) 

然后创建一个新目录并使用 shutil.copy 将随机 selected 图像移动到这个新目录

#creates a new directory 
os.mkdir('testDirectory')

#moves the randomly selected image into new directory 
shutil.copy(randomImages, testDirectory)

但是,我收到以下错误:

Traceback (most recent call last):
  File "crawl.py", line 28, in <module>
    randomImages = random.choice(os.listdir(imagePath)) 
TypeError: coercing to Unicode: need string or buffer, generator found

我也试过了


for root, dirs, files in os.walk("/Path/to/Directory", topdown=False):
    imagePath = ("/Path/to/Directory") #creates a variable that lets me access all img files in different folders
    randomImages = random.choice(os.listdir(imagePath)) 
    print randomImages

但是这个 returns 随机 selection 子目录(不是其中的图像)以及 .ds 存储文件。

您应该能够向 shutil.copy() 提供源文件路径和目标文件路径。在我看来,您已经有了一个文件列表,因此您可以直接复制它们。

import os
import random
import shutil


files_list = []
for root, dirs, files in os.walk("/Path/to/Directory"):
    for file in files:
        #all 
        if file.endswith(".jpg") or file.endswith(".png") or file.endswith(".jpeg"):
            files_list.append(os.path.join(root, file))


#print images
#lets me count and print the amount of jpeg,jpg,pmg 
file_count = len(files_list)
print file_count

print files_list   
selected_files = random.sample(files_list, 2))  #assign to a list

dest_path = "/path/to/new/folder/"
os.mkdir(dest_path)

for src_path in selected_files:
    shutil.copy(src_path, os.path.join(dest_path, os.path.basename(src_path)))

这是代码,我猜你想移动文件而不是制作另一个副本。

import os
import random
import shutil


files_list = []

for root, dirs, files in os.walk("<SOURCE_DIR>"):
    for file in files:
        #all 
        if file.endswith(".jpg") or file.endswith(".png") or file.endswith(".jpeg"):
            files_list.append(os.path.join(root, file))


#print images
#lets me count and print the amount of jpeg,jpg,pmg 
file_count = len(files_list)
print file_count

# print files_list   
filesToCopy = random.sample(files_list, 2)  #prints two random files from list 

destPath = "<DESTINATION_DIR>"

# if destination dir does not exists, create it
if os.path.isdir(destPath) == False:
        os.makedirs(destPath)

# iteraate over all random files and move them
for file in filesToCopy:
    shutil.move(file, destPath)