将随机选择的图像放入新折叠中

2024-10-03 02:39:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个包含大量子目录的目录。你知道吗

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

我想:

从这些子目录中选择X数量的随机图像

创建一个新文件夹并将这些随机选择的图像复制到其中。你知道吗

感谢这里已经收到的帮助,我可以使用os.walkrandom.choice打印出随机选择的图像。你知道吗

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 

但是,我的问题是如何选择随机图像(而不是它们的名称)

我试图创建一个变量imagePath,使用os.步行你知道吗

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

以及一个新变量,用于从imagePath中随机选择单个图像

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

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

#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

但这会返回随机选择的子目录(而不是其中的图像)以及.ds存储文件。你知道吗


Tags: topathin图像osrandomfilesdirectory
2条回答

你应该能吃东西shutil.副本()源和目标文件路径。在我看来,你已经有一个文件列表,所以你可以复制它们。你知道吗

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)

相关问题 更多 >