Last active
November 2, 2020 18:31
-
-
Save tomahim/75f1c24dbdbe24dea4e467c26ae2ac80 to your computer and use it in GitHub Desktop.
Pick random files from a folder and read them
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 | |
# our folder path containing some images | |
folder_path = 'images/cats' | |
# the number of file to generate | |
num_files_desired = 1000 | |
# loop on all files of the folder and build a list of files paths | |
images = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))] | |
num_generated_files = 0 | |
while num_generated_files <= num_files_desired: | |
# random image from the folder | |
image_path = random.choice(images) | |
# read image as an two dimensional array of pixels | |
image_to_transform = sk.io.imread(image_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the while loop num_generated_files is never incremented, so it becomes an infinite loop. If you add "num_generated_files+=1" inside the loop, the code will work the way you intended.