Skip to content

Instantly share code, notes, and snippets.

@tomahim
Last active November 2, 2020 18:31
Show Gist options
  • Save tomahim/75f1c24dbdbe24dea4e467c26ae2ac80 to your computer and use it in GitHub Desktop.
Save tomahim/75f1c24dbdbe24dea4e467c26ae2ac80 to your computer and use it in GitHub Desktop.
Pick random files from a folder and read them
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)
@girishpillai17
Copy link

When i run the code on jupyter notebook its never stops. What might be the issue?

@himanshu2602
Copy link

When i run the code on jupyter notebook its never stops. What might be the issue?

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment